From d201456903f3ecae1f7794edfab0d5678e642265 Mon Sep 17 00:00:00 2001 From: shiqian Date: Thu, 3 Jul 2008 22:38:12 +0000 Subject: Initial import. --- include/gtest/gtest.h | 1242 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1242 insertions(+) create mode 100644 include/gtest/gtest.h (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h new file mode 100644 index 00000000..8e857c8d --- /dev/null +++ b/include/gtest/gtest.h @@ -0,0 +1,1242 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Author: wan@google.com (Zhanyong Wan) +// +// The Google C++ Testing Framework (Google Test) +// +// This header file defines the public API for Google Test. It should be +// included by any test program that uses Google Test. +// +// IMPORTANT NOTE: Due to limitation of the C++ language, we have to +// leave some internal implementation details in this header file. +// They are clearly marked by comments like this: +// +// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +// +// Such code is NOT meant to be used by a user directly, and is subject +// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user +// program! +// +// Acknowledgment: Google Test borrowed the idea of automatic test +// registration from Barthelemy Dagenais' (barthelemy@prologique.com) +// easyUnit framework. + +#ifndef GTEST_INCLUDE_GTEST_GTEST_H_ +#define GTEST_INCLUDE_GTEST_GTEST_H_ + +// The following platform macros are used throughout Google Test: +// _WIN32_WCE Windows CE (set in project files) +// __SYMBIAN32__ Symbian (set by Symbian tool chain) +// +// Note that even though _MSC_VER and _WIN32_WCE really indicate a compiler +// and a Win32 implementation, respectively, we use them to indicate the +// combination of compiler - Win 32 API - C library, since the code currently +// only supports: +// Windows proper with Visual C++ and MS C library (_MSC_VER && !_WIN32_WCE) and +// Windows Mobile with Visual C++ and no C library (_WIN32_WCE). + +#if defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) +// When using Google Test on the Mac as a framework, all the includes +// will be in the framework headers folder along with gtest.h. Define +// GTEST_NOT_MAC_FRAMEWORK_MODE if you are building Google Test on the +// Mac and are not using it as a framework. More info on frameworks +// available here: +// http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/ +// Concepts/WhatAreFrameworks.html. +#include "gtest-death-test.h" // NOLINT +#include "gtest-internal.h" // NOLINT +#include "gtest-message.h" // NOLINT +#include "gtest-string.h" // NOLINT +#include "gtest_prod.h" // NOLINT +#else +#include +#include +#include +#include +#include +#endif // defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) + +// Depending on the platform, different string classes are available. +// On Windows, ::std::string compiles only when exceptions are +// enabled. On Linux, in addition to ::std::string, Google also makes +// use of class ::string, which has the same interface as +// ::std::string, but has a different implementation. +// +// The user can tell us whether ::std::string is available in his +// environment by defining the macro GTEST_HAS_STD_STRING to either 1 +// or 0 on the compiler command line. He can also define +// GTEST_HAS_GLOBAL_STRING to 1 to indicate that ::string is available +// AND is a distinct type to ::std::string, or define it to 0 to +// indicate otherwise. +// +// If the user's ::std::string and ::string are the same class due to +// aliasing, he should define GTEST_HAS_STD_STRING to 1 and +// GTEST_HAS_GLOBAL_STRING to 0. +// +// If the user doesn't define GTEST_HAS_STD_STRING and/or +// GTEST_HAS_GLOBAL_STRING, they are defined heuristically. + +namespace testing { + +// The upper limit for valid stack trace depths. +const int kMaxStackTraceDepth = 100; + +// This flag specifies the maximum number of stack frames to be +// printed in a failure message. +GTEST_DECLARE_int32(stack_trace_depth); + +// This flag controls whether Google Test includes Google Test internal +// stack frames in failure stack traces. +GTEST_DECLARE_bool(show_internal_stack_frames); + +// The possible outcomes of a test part (i.e. an assertion or an +// explicit SUCCEED(), FAIL(), or ADD_FAILURE()). +enum TestPartResultType { + TPRT_SUCCESS, // Succeeded. + TPRT_NONFATAL_FAILURE, // Failed but the test can continue. + TPRT_FATAL_FAILURE // Failed and the test should be terminated. +}; + +namespace internal { + +class GTestFlagSaver; + +// Converts a streamable value to a String. A NULL pointer is +// converted to "(null)". When the input value is a ::string, +// ::std::string, ::wstring, or ::std::wstring object, each NUL +// character in it is replaced with "\\0". +// Declared in gtest-internal.h but defined here, so that it has access +// to the definition of the Message class, required by the ARM +// compiler. +template +String StreamableToString(const T& streamable) { + return (Message() << streamable).GetString(); +} + +} // namespace internal + +// A class for indicating whether an assertion was successful. When +// the assertion wasn't successful, the AssertionResult object +// remembers a non-empty message that described how it failed. +// +// This class is useful for defining predicate-format functions to be +// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). +// +// The constructor of AssertionResult is private. To create an +// instance of this class, use one of the factory functions +// (AssertionSuccess() and AssertionFailure()). +// +// For example, in order to be able to write: +// +// // Verifies that Foo() returns an even number. +// EXPECT_PRED_FORMAT1(IsEven, Foo()); +// +// you just need to define: +// +// testing::AssertionResult IsEven(const char* expr, int n) { +// if ((n % 2) == 0) return testing::AssertionSuccess(); +// +// Message msg; +// msg << "Expected: " << expr << " is even\n" +// << " Actual: it's " << n; +// return testing::AssertionFailure(msg); +// } +// +// If Foo() returns 5, you will see the following message: +// +// Expected: Foo() is even +// Actual: it's 5 +class AssertionResult { + public: + // Declares factory functions for making successful and failed + // assertion results as friends. + friend AssertionResult AssertionSuccess(); + friend AssertionResult AssertionFailure(const Message&); + + // Returns true iff the assertion succeeded. + operator bool() const { return failure_message_.c_str() == NULL; } // NOLINT + + // Returns the assertion's failure message. + const char* failure_message() const { return failure_message_.c_str(); } + + private: + // The default constructor. It is used when the assertion succeeded. + AssertionResult() {} + + // The constructor used when the assertion failed. + explicit AssertionResult(const internal::String& failure_message); + + // Stores the assertion's failure message. + internal::String failure_message_; +}; + +// Makes a successful assertion result. +AssertionResult AssertionSuccess(); + +// Makes a failed assertion result with the given failure message. +AssertionResult AssertionFailure(const Message& msg); + +// The abstract class that all tests inherit from. +// +// In Google Test, a unit test program contains one or many TestCases, and +// each TestCase contains one or many Tests. +// +// When you define a test using the TEST macro, you don't need to +// explicitly derive from Test - the TEST macro automatically does +// this for you. +// +// The only time you derive from Test is when defining a test fixture +// to be used a TEST_F. For example: +// +// class FooTest : public testing::Test { +// protected: +// virtual void SetUp() { ... } +// virtual void TearDown() { ... } +// ... +// }; +// +// TEST_F(FooTest, Bar) { ... } +// TEST_F(FooTest, Baz) { ... } +// +// Test is not copyable. +class Test { + public: + friend class internal::TestInfoImpl; + + // Defines types for pointers to functions that set up and tear down + // a test case. + typedef void (*SetUpTestCaseFunc)(); + typedef void (*TearDownTestCaseFunc)(); + + // The d'tor is virtual as we intend to inherit from Test. + virtual ~Test(); + + // Returns true iff the current test has a fatal failure. + static bool HasFatalFailure(); + + // Logs a property for the current test. Only the last value for a given + // key is remembered. + // These are public static so they can be called from utility functions + // that are not members of the test fixture. + // The arguments are const char* instead strings, as Google Test is used + // on platforms where string doesn't compile. + // + // Note that a driving consideration for these RecordProperty methods + // was to produce xml output suited to the Greenspan charting utility, + // which at present will only chart values that fit in a 32-bit int. It + // is the user's responsibility to restrict their values to 32-bit ints + // if they intend them to be used with Greenspan. + static void RecordProperty(const char* key, const char* value); + static void RecordProperty(const char* key, int value); + + protected: + // Creates a Test object. + Test(); + + // Sets up the stuff shared by all tests in this test case. + // + // Google Test will call Foo::SetUpTestCase() before running the first + // test in test case Foo. Hence a sub-class can define its own + // SetUpTestCase() method to shadow the one defined in the super + // class. + static void SetUpTestCase() {} + + // Tears down the stuff shared by all tests in this test case. + // + // Google Test will call Foo::TearDownTestCase() after running the last + // test in test case Foo. Hence a sub-class can define its own + // TearDownTestCase() method to shadow the one defined in the super + // class. + static void TearDownTestCase() {} + + // Sets up the test fixture. + virtual void SetUp(); + + // Tears down the test fixture. + virtual void TearDown(); + + private: + // Returns true iff the current test has the same fixture class as + // the first test in the current test case. + static bool HasSameFixtureClass(); + + // Runs the test after the test fixture has been set up. + // + // A sub-class must implement this to define the test logic. + // + // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. + // Instead, use the TEST or TEST_F macro. + virtual void TestBody() = 0; + + // Sets up, executes, and tears down the test. + void Run(); + + // Uses a GTestFlagSaver to save and restore all Google Test flags. + const internal::GTestFlagSaver* const gtest_flag_saver_; + + // Often a user mis-spells SetUp() as Setup() and spends a long time + // wondering why it is never called by Google Test. The declaration of + // the following method is solely for catching such an error at + // compile time: + // + // - The return type is deliberately chosen to be not void, so it + // will be a conflict if a user declares void Setup() in his test + // fixture. + // + // - This method is private, so it will be another compiler error + // if a user calls it from his test fixture. + // + // DO NOT OVERRIDE THIS FUNCTION. + // + // If you see an error about overriding the following function or + // about it being private, you have mis-spelled SetUp() as Setup(). + struct Setup_should_be_spelled_SetUp {}; + virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } + + // We disallow copying Tests. + GTEST_DISALLOW_COPY_AND_ASSIGN(Test); +}; + + +// Defines the type of a function pointer that creates a Test object +// when invoked. +typedef Test* (*TestMaker)(); + + +// A TestInfo object stores the following information about a test: +// +// Test case name +// Test name +// Whether the test should be run +// A function pointer that creates the test object when invoked +// Test result +// +// The constructor of TestInfo registers itself with the UnitTest +// singleton such that the RUN_ALL_TESTS() macro knows which tests to +// run. +class TestInfo { + public: + // Destructs a TestInfo object. This function is not virtual, so + // don't inherit from TestInfo. + ~TestInfo(); + + // Creates a TestInfo object and registers it with the UnitTest + // singleton; returns the created object. + // + // Arguments: + // + // test_case_name: name of the test case + // name: name of the test + // fixture_class_id: ID of the test fixture class + // set_up_tc: pointer to the function that sets up the test case + // tear_down_tc: pointer to the function that tears down the test case + // maker: pointer to the function that creates a test object + // + // This is public only because it's needed by the TEST and TEST_F macros. + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + static TestInfo* MakeAndRegisterInstance( + const char* test_case_name, + const char* name, + internal::TypeId fixture_class_id, + Test::SetUpTestCaseFunc set_up_tc, + Test::TearDownTestCaseFunc tear_down_tc, + TestMaker maker); + + // Returns the test case name. + const char* test_case_name() const; + + // Returns the test name. + const char* name() const; + + // Returns true if this test should run. + // + // Google Test allows the user to filter the tests by their full names. + // The full name of a test Bar in test case Foo is defined as + // "Foo.Bar". Only the tests that match the filter will run. + // + // A filter is a colon-separated list of glob (not regex) patterns, + // optionally followed by a '-' and a colon-separated list of + // negative patterns (tests to exclude). A test is run if it + // matches one of the positive patterns and does not match any of + // the negative patterns. + // + // For example, *A*:Foo.* is a filter that matches any string that + // contains the character 'A' or starts with "Foo.". + bool should_run() const; + + // Returns the result of the test. + const internal::TestResult* result() const; + private: +#ifdef GTEST_HAS_DEATH_TEST + friend class internal::DefaultDeathTestFactory; +#endif // GTEST_HAS_DEATH_TEST + friend class internal::TestInfoImpl; + friend class internal::UnitTestImpl; + friend class Test; + friend class TestCase; + + // Increments the number of death tests encountered in this test so + // far. + int increment_death_test_count(); + + // Accessors for the implementation object. + internal::TestInfoImpl* impl() { return impl_; } + const internal::TestInfoImpl* impl() const { return impl_; } + + // Constructs a TestInfo object. + TestInfo(const char* test_case_name, const char* name, + internal::TypeId fixture_class_id, TestMaker maker); + + // An opaque implementation object. + internal::TestInfoImpl* impl_; + + GTEST_DISALLOW_COPY_AND_ASSIGN(TestInfo); +}; + +// An Environment object is capable of setting up and tearing down an +// environment. The user should subclass this to define his own +// environment(s). +// +// An Environment object does the set-up and tear-down in virtual +// methods SetUp() and TearDown() instead of the constructor and the +// destructor, as: +// +// 1. You cannot safely throw from a destructor. This is a problem +// as in some cases Google Test is used where exceptions are enabled, and +// we may want to implement ASSERT_* using exceptions where they are +// available. +// 2. You cannot use ASSERT_* directly in a constructor or +// destructor. +class Environment { + public: + // The d'tor is virtual as we need to subclass Environment. + virtual ~Environment() {} + + // Override this to define how to set up the environment. + virtual void SetUp() {} + + // Override this to define how to tear down the environment. + virtual void TearDown() {} + private: + // If you see an error about overriding the following function or + // about it being private, you have mis-spelled SetUp() as Setup(). + struct Setup_should_be_spelled_SetUp {}; + virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } +}; + +// A UnitTest consists of a list of TestCases. +// +// This is a singleton class. The only instance of UnitTest is +// created when UnitTest::GetInstance() is first called. This +// instance is never deleted. +// +// UnitTest is not copyable. +// +// This class is thread-safe as long as the methods are called +// according to their specification. +class UnitTest { + public: + // Gets the singleton UnitTest object. The first time this method + // is called, a UnitTest object is constructed and returned. + // Consecutive calls will return the same object. + static UnitTest* GetInstance(); + + // Registers and returns a global test environment. When a test + // program is run, all global test environments will be set-up in + // the order they were registered. After all tests in the program + // have finished, all global test environments will be torn-down in + // the *reverse* order they were registered. + // + // The UnitTest object takes ownership of the given environment. + // + // This method can only be called from the main thread. + Environment* AddEnvironment(Environment* env); + + // Adds a TestPartResult to the current TestResult object. All + // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) + // eventually call this to report their results. The user code + // should use the assertion macros instead of calling this directly. + // + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + void AddTestPartResult(TestPartResultType result_type, + const char* file_name, + int line_number, + const internal::String& message, + const internal::String& os_stack_trace); + + // Adds a TestProperty to the current TestResult object. If the result already + // contains a property with the same key, the value will be updated. + void RecordPropertyForCurrentTest(const char* key, const char* value); + + // Runs all tests in this UnitTest object and prints the result. + // Returns 0 if successful, or 1 otherwise. + // + // This method can only be called from the main thread. + // + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + int Run() GTEST_MUST_USE_RESULT; + + // Returns the TestCase object for the test that's currently running, + // or NULL if no test is running. + const TestCase* current_test_case() const; + + // Returns the TestInfo object for the test that's currently running, + // or NULL if no test is running. + const TestInfo* current_test_info() const; + + // Accessors for the implementation object. + internal::UnitTestImpl* impl() { return impl_; } + const internal::UnitTestImpl* impl() const { return impl_; } + private: + // ScopedTrace is a friend as it needs to modify the per-thread + // trace stack, which is a private member of UnitTest. + friend class internal::ScopedTrace; + + // Creates an empty UnitTest. + UnitTest(); + + // D'tor + virtual ~UnitTest(); + + // Pushes a trace defined by SCOPED_TRACE() on to the per-thread + // Google Test trace stack. + void PushGTestTrace(const internal::TraceInfo& trace); + + // Pops a trace from the per-thread Google Test trace stack. + void PopGTestTrace(); + + // Protects mutable state in *impl_. This is mutable as some const + // methods need to lock it too. + mutable internal::Mutex mutex_; + + // Opaque implementation object. This field is never changed once + // the object is constructed. We don't mark it as const here, as + // doing so will cause a warning in the constructor of UnitTest. + // Mutable state in *impl_ is protected by mutex_. + internal::UnitTestImpl* impl_; + + // We disallow copying UnitTest. + GTEST_DISALLOW_COPY_AND_ASSIGN(UnitTest); +}; + +// A convenient wrapper for adding an environment for the test +// program. +// +// You should call this before RUN_ALL_TESTS() is called, probably in +// main(). If you use gtest_main, you need to call this before main() +// starts for it to take effect. For example, you can define a global +// variable like this: +// +// testing::Environment* const foo_env = +// testing::AddGlobalTestEnvironment(new FooEnvironment); +// +// However, we strongly recommend you to write your own main() and +// call AddGlobalTestEnvironment() there, as relying on initialization +// of global variables makes the code harder to read and may cause +// problems when you register multiple environments from different +// translation units and the environments have dependencies among them +// (remember that the compiler doesn't guarantee the order in which +// global variables from different translation units are initialized). +inline Environment* AddGlobalTestEnvironment(Environment* env) { + return UnitTest::GetInstance()->AddEnvironment(env); +} + +// Initializes Google Test. This must be called before calling +// RUN_ALL_TESTS(). In particular, it parses a command line for the +// flags that Google Test recognizes. Whenever a Google Test flag is +// seen, it is removed from argv, and *argc is decremented. +// +// No value is returned. Instead, the Google Test flag variables are +// updated. +void InitGoogleTest(int* argc, char** argv); + +// This overloaded version can be used in Windows programs compiled in +// UNICODE mode. +#ifdef GTEST_OS_WINDOWS +void InitGoogleTest(int* argc, wchar_t** argv); +#endif // GTEST_OS_WINDOWS + +namespace internal { + +// These overloaded versions handle ::std::string and ::std::wstring. +#if GTEST_HAS_STD_STRING +inline String FormatForFailureMessage(const ::std::string& str) { + return (Message() << '"' << str << '"').GetString(); +} +#endif // GTEST_HAS_STD_STRING + +#if GTEST_HAS_STD_WSTRING +inline String FormatForFailureMessage(const ::std::wstring& wstr) { + return (Message() << "L\"" << wstr << '"').GetString(); +} +#endif // GTEST_HAS_STD_WSTRING + +// These overloaded versions handle ::string and ::wstring. +#if GTEST_HAS_GLOBAL_STRING +inline String FormatForFailureMessage(const ::string& str) { + return (Message() << '"' << str << '"').GetString(); +} +#endif // GTEST_HAS_GLOBAL_STRING + +#if GTEST_HAS_GLOBAL_WSTRING +inline String FormatForFailureMessage(const ::wstring& wstr) { + return (Message() << "L\"" << wstr << '"').GetString(); +} +#endif // GTEST_HAS_GLOBAL_WSTRING + +// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) +// operand to be used in a failure message. The type (but not value) +// of the other operand may affect the format. This allows us to +// print a char* as a raw pointer when it is compared against another +// char*, and print it as a C string when it is compared against an +// std::string object, for example. +// +// The default implementation ignores the type of the other operand. +// Some specialized versions are used to handle formatting wide or +// narrow C strings. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +template +String FormatForComparisonFailureMessage(const T1& value, + const T2& /* other_operand */) { + return FormatForFailureMessage(value); +} + +// The helper function for {ASSERT|EXPECT}_EQ. +template +AssertionResult CmpHelperEQ(const char* expected_expression, + const char* actual_expression, + const T1& expected, + const T2& actual) { + if (expected == actual) { + return AssertionSuccess(); + } + + return EqFailure(expected_expression, + actual_expression, + FormatForComparisonFailureMessage(expected, actual), + FormatForComparisonFailureMessage(actual, expected), + false); +} + +// With this overloaded version, we allow anonymous enums to be used +// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums +// can be implicitly cast to BiggestInt. +AssertionResult CmpHelperEQ(const char* expected_expression, + const char* actual_expression, + BiggestInt expected, + BiggestInt actual); + +// The helper class for {ASSERT|EXPECT}_EQ. The template argument +// lhs_is_null_literal is true iff the first argument to ASSERT_EQ() +// is a null pointer literal. The following default implementation is +// for lhs_is_null_literal being false. +template +class EqHelper { + public: + // This templatized version is for the general case. + template + static AssertionResult Compare(const char* expected_expression, + const char* actual_expression, + const T1& expected, + const T2& actual) { + return CmpHelperEQ(expected_expression, actual_expression, expected, + actual); + } + + // With this overloaded version, we allow anonymous enums to be used + // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous + // enums can be implicitly cast to BiggestInt. + // + // Even though its body looks the same as the above version, we + // cannot merge the two, as it will make anonymous enums unhappy. + static AssertionResult Compare(const char* expected_expression, + const char* actual_expression, + BiggestInt expected, + BiggestInt actual) { + return CmpHelperEQ(expected_expression, actual_expression, expected, + actual); + } +}; + +// This specialization is used when the first argument to ASSERT_EQ() +// is a null pointer literal. +template <> +class EqHelper { + public: + // We define two overloaded versions of Compare(). The first + // version will be picked when the second argument to ASSERT_EQ() is + // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or + // EXPECT_EQ(false, a_bool). + template + static AssertionResult Compare(const char* expected_expression, + const char* actual_expression, + const T1& expected, + const T2& actual) { + return CmpHelperEQ(expected_expression, actual_expression, expected, + actual); + } + + // This version will be picked when the second argument to + // ASSERT_EQ() is a pointer, e.g. ASSERT_EQ(NULL, a_pointer). + template + static AssertionResult Compare(const char* expected_expression, + const char* actual_expression, + const T1& expected, + T2* actual) { + // We already know that 'expected' is a null pointer. + return CmpHelperEQ(expected_expression, actual_expression, + static_cast(NULL), actual); + } +}; + +// A macro for implementing the helper functions needed to implement +// ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste +// of similar code. +// +// For each templatized helper function, we also define an overloaded +// version for BiggestInt in order to reduce code bloat and allow +// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled +// with gcc 4. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +#define GTEST_IMPL_CMP_HELPER(op_name, op)\ +template \ +AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ + const T1& val1, const T2& val2) {\ + if (val1 op val2) {\ + return AssertionSuccess();\ + } else {\ + Message msg;\ + msg << "Expected: (" << expr1 << ") " #op " (" << expr2\ + << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ + << " vs " << FormatForComparisonFailureMessage(val2, val1);\ + return AssertionFailure(msg);\ + }\ +}\ +AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ + BiggestInt val1, BiggestInt val2); + +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + +// Implements the helper function for {ASSERT|EXPECT}_NE +GTEST_IMPL_CMP_HELPER(NE, !=) +// Implements the helper function for {ASSERT|EXPECT}_LE +GTEST_IMPL_CMP_HELPER(LE, <=) +// Implements the helper function for {ASSERT|EXPECT}_LT +GTEST_IMPL_CMP_HELPER(LT, < ) +// Implements the helper function for {ASSERT|EXPECT}_GE +GTEST_IMPL_CMP_HELPER(GE, >=) +// Implements the helper function for {ASSERT|EXPECT}_GT +GTEST_IMPL_CMP_HELPER(GT, > ) + +#undef GTEST_IMPL_CMP_HELPER + +// The helper function for {ASSERT|EXPECT}_STREQ. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult CmpHelperSTREQ(const char* expected_expression, + const char* actual_expression, + const char* expected, + const char* actual); + +// The helper function for {ASSERT|EXPECT}_STRCASEEQ. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, + const char* actual_expression, + const char* expected, + const char* actual); + +// The helper function for {ASSERT|EXPECT}_STRNE. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, + const char* s1, + const char* s2); + +// The helper function for {ASSERT|EXPECT}_STRCASENE. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult CmpHelperSTRCASENE(const char* s1_expression, + const char* s2_expression, + const char* s1, + const char* s2); + + +// Helper function for *_STREQ on wide strings. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult CmpHelperSTREQ(const char* expected_expression, + const char* actual_expression, + const wchar_t* expected, + const wchar_t* actual); + +// Helper function for *_STRNE on wide strings. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, + const wchar_t* s1, + const wchar_t* s2); + +} // namespace internal + +// IsSubstring() and IsNotSubstring() are intended to be used as the +// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by +// themselves. They check whether needle is a substring of haystack +// (NULL is considered a substring of itself only), and return an +// appropriate error message when they fail. +// +// The {needle,haystack}_expr arguments are the stringified +// expressions that generated the two real arguments. +AssertionResult IsSubstring( + const char* needle_expr, const char* haystack_expr, + const char* needle, const char* haystack); +AssertionResult IsSubstring( + const char* needle_expr, const char* haystack_expr, + const wchar_t* needle, const wchar_t* haystack); +AssertionResult IsNotSubstring( + const char* needle_expr, const char* haystack_expr, + const char* needle, const char* haystack); +AssertionResult IsNotSubstring( + const char* needle_expr, const char* haystack_expr, + const wchar_t* needle, const wchar_t* haystack); +#if GTEST_HAS_STD_STRING +AssertionResult IsSubstring( + const char* needle_expr, const char* haystack_expr, + const ::std::string& needle, const ::std::string& haystack); +AssertionResult IsNotSubstring( + const char* needle_expr, const char* haystack_expr, + const ::std::string& needle, const ::std::string& haystack); +#endif // GTEST_HAS_STD_STRING + +#if GTEST_HAS_STD_WSTRING +AssertionResult IsSubstring( + const char* needle_expr, const char* haystack_expr, + const ::std::wstring& needle, const ::std::wstring& haystack); +AssertionResult IsNotSubstring( + const char* needle_expr, const char* haystack_expr, + const ::std::wstring& needle, const ::std::wstring& haystack); +#endif // GTEST_HAS_STD_WSTRING + +namespace internal { + +// Helper template function for comparing floating-points. +// +// Template parameter: +// +// RawType: the raw floating-point type (either float or double) +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +template +AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression, + const char* actual_expression, + RawType expected, + RawType actual) { + const FloatingPoint lhs(expected), rhs(actual); + + if (lhs.AlmostEquals(rhs)) { + return AssertionSuccess(); + } + + StrStream expected_ss; + expected_ss << std::setprecision(std::numeric_limits::digits10 + 2) + << expected; + + StrStream actual_ss; + actual_ss << std::setprecision(std::numeric_limits::digits10 + 2) + << actual; + + return EqFailure(expected_expression, + actual_expression, + StrStreamToString(&expected_ss), + StrStreamToString(&actual_ss), + false); +} + +// Helper function for implementing ASSERT_NEAR. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +AssertionResult DoubleNearPredFormat(const char* expr1, + const char* expr2, + const char* abs_error_expr, + double val1, + double val2, + double abs_error); + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// A class that enables one to stream messages to assertion macros +class AssertHelper { + public: + // Constructor. + AssertHelper(TestPartResultType type, const char* file, int line, + const char* message); + // Message assignment is a semantic trick to enable assertion + // streaming; see the GTEST_MESSAGE macro below. + void operator=(const Message& message) const; + private: + TestPartResultType const type_; + const char* const file_; + int const line_; + String const message_; + + GTEST_DISALLOW_COPY_AND_ASSIGN(AssertHelper); +}; + +} // namespace internal + +// Macros for indicating success/failure in test code. + +// ADD_FAILURE unconditionally adds a failure to the current test. +// SUCCEED generates a success - it doesn't automatically make the +// current test successful, as a test is only successful when it has +// no failure. +// +// EXPECT_* verifies that a certain condition is satisfied. If not, +// it behaves like ADD_FAILURE. In particular: +// +// EXPECT_TRUE verifies that a Boolean condition is true. +// EXPECT_FALSE verifies that a Boolean condition is false. +// +// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except +// that they will also abort the current function on failure. People +// usually want the fail-fast behavior of FAIL and ASSERT_*, but those +// writing data-driven tests often find themselves using ADD_FAILURE +// and EXPECT_* more. +// +// Examples: +// +// EXPECT_TRUE(server.StatusIsOK()); +// ASSERT_FALSE(server.HasPendingRequest(port)) +// << "There are still pending requests " << "on port " << port; + +// Generates a nonfatal failure with a generic message. +#define ADD_FAILURE() GTEST_NONFATAL_FAILURE("Failed") + +// Generates a fatal failure with a generic message. +#define FAIL() GTEST_FATAL_FAILURE("Failed") + +// Generates a success with a generic message. +#define SUCCEED() GTEST_SUCCESS("Succeeded") + +// Boolean assertions. +#define EXPECT_TRUE(condition) \ + GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ + GTEST_NONFATAL_FAILURE) +#define EXPECT_FALSE(condition) \ + GTEST_TEST_BOOLEAN(!(condition), #condition, true, false, \ + GTEST_NONFATAL_FAILURE) +#define ASSERT_TRUE(condition) \ + GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ + GTEST_FATAL_FAILURE) +#define ASSERT_FALSE(condition) \ + GTEST_TEST_BOOLEAN(!(condition), #condition, true, false, \ + GTEST_FATAL_FAILURE) + +// Includes the auto-generated header that implements a family of +// generic predicate assertion macros. +#if defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) +// When using Google Test on the Mac as a framework, all the includes will be +// in the framework headers folder along with gtest.h. +// Define GTEST_NOT_MAC_FRAMEWORK_MODE if you are building Google Test on +// the Mac and are not using it as a framework. +// More info on frameworks available here: +// http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/ +// Concepts/WhatAreFrameworks.html. +#include "gtest_pred_impl.h" // NOLINT +#else +#include +#endif // defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) + +// Macros for testing equalities and inequalities. +// +// * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual +// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 +// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 +// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 +// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 +// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 +// +// When they are not, Google Test prints both the tested expressions and +// their actual values. The values must be compatible built-in types, +// or you will get a compiler error. By "compatible" we mean that the +// values can be compared by the respective operator. +// +// Note: +// +// 1. It is possible to make a user-defined type work with +// {ASSERT|EXPECT}_??(), but that requires overloading the +// comparison operators and is thus discouraged by the Google C++ +// Usage Guide. Therefore, you are advised to use the +// {ASSERT|EXPECT}_TRUE() macro to assert that two objects are +// equal. +// +// 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on +// pointers (in particular, C strings). Therefore, if you use it +// with two C strings, you are testing how their locations in memory +// are related, not how their content is related. To compare two C +// strings by content, use {ASSERT|EXPECT}_STR*(). +// +// 3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to +// {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you +// what the actual value is when it fails, and similarly for the +// other comparisons. +// +// 4. Do not depend on the order in which {ASSERT|EXPECT}_??() +// evaluate their arguments, which is undefined. +// +// 5. These macros evaluate their arguments exactly once. +// +// Examples: +// +// EXPECT_NE(5, Foo()); +// EXPECT_EQ(NULL, a_pointer); +// ASSERT_LT(i, array_size); +// ASSERT_GT(records.size(), 0) << "There is no record left."; + +#define EXPECT_EQ(expected, actual) \ + EXPECT_PRED_FORMAT2(::testing::internal:: \ + EqHelper::Compare, \ + expected, actual) +#define EXPECT_NE(expected, actual) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual) +#define EXPECT_LE(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) +#define EXPECT_LT(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) +#define EXPECT_GE(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) +#define EXPECT_GT(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) + +#define ASSERT_EQ(expected, actual) \ + ASSERT_PRED_FORMAT2(::testing::internal:: \ + EqHelper::Compare, \ + expected, actual) +#define ASSERT_NE(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) +#define ASSERT_LE(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) +#define ASSERT_LT(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) +#define ASSERT_GE(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) +#define ASSERT_GT(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) + +// C String Comparisons. All tests treat NULL and any non-NULL string +// as different. Two NULLs are equal. +// +// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 +// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 +// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case +// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case +// +// For wide or narrow string objects, you can use the +// {ASSERT|EXPECT}_??() macros. +// +// Don't depend on the order in which the arguments are evaluated, +// which is undefined. +// +// These macros evaluate their arguments exactly once. + +#define EXPECT_STREQ(expected, actual) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) +#define EXPECT_STRNE(s1, s2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) +#define EXPECT_STRCASEEQ(expected, actual) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) +#define EXPECT_STRCASENE(s1, s2)\ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) + +#define ASSERT_STREQ(expected, actual) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) +#define ASSERT_STRNE(s1, s2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) +#define ASSERT_STRCASEEQ(expected, actual) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) +#define ASSERT_STRCASENE(s1, s2)\ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) + +// Macros for comparing floating-point numbers. +// +// * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual): +// Tests that two float values are almost equal. +// * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual): +// Tests that two double values are almost equal. +// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): +// Tests that v1 and v2 are within the given distance to each other. +// +// Google Test uses ULP-based comparison to automatically pick a default +// error bound that is appropriate for the operands. See the +// FloatingPoint template class in gtest-internal.h if you are +// interested in the implementation details. + +#define EXPECT_FLOAT_EQ(expected, actual)\ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + expected, actual) + +#define EXPECT_DOUBLE_EQ(expected, actual)\ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + expected, actual) + +#define ASSERT_FLOAT_EQ(expected, actual)\ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + expected, actual) + +#define ASSERT_DOUBLE_EQ(expected, actual)\ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + expected, actual) + +#define EXPECT_NEAR(val1, val2, abs_error)\ + EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ + val1, val2, abs_error) + +#define ASSERT_NEAR(val1, val2, abs_error)\ + ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ + val1, val2, abs_error) + +// These predicate format functions work on floating-point values, and +// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. +// +// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); + +// Asserts that val1 is less than, or almost equal to, val2. Fails +// otherwise. In particular, it fails if either val1 or val2 is NaN. +AssertionResult FloatLE(const char* expr1, const char* expr2, + float val1, float val2); +AssertionResult DoubleLE(const char* expr1, const char* expr2, + double val1, double val2); + + +#ifdef GTEST_OS_WINDOWS + +// Macros that test for HRESULT failure and success, these are only useful +// on Windows, and rely on Windows SDK macros and APIs to compile. +// +// * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) +// +// When expr unexpectedly fails or succeeds, Google Test prints the expected result +// and the actual result with both a human-readable string representation of +// the error, if available, as well as the hex result code. +#define EXPECT_HRESULT_SUCCEEDED(expr) \ + EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) + +#define ASSERT_HRESULT_SUCCEEDED(expr) \ + ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) + +#define EXPECT_HRESULT_FAILED(expr) \ + EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) + +#define ASSERT_HRESULT_FAILED(expr) \ + ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) + +#endif // GTEST_OS_WINDOWS + + +// Causes a trace (including the source file path, the current line +// number, and the given message) to be included in every test failure +// message generated by code in the current scope. The effect is +// undone when the control leaves the current scope. +// +// The message argument can be anything streamable to std::ostream. +// +// In the implementation, we include the current line number as part +// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s +// to appear in the same block - as long as they are on different +// lines. +#define SCOPED_TRACE(message) \ + ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN(gtest_trace_, __LINE__)(\ + __FILE__, __LINE__, ::testing::Message() << (message)) + + +// Defines a test. +// +// The first parameter is the name of the test case, and the second +// parameter is the name of the test within the test case. +// +// The convention is to end the test case name with "Test". For +// example, a test case for the Foo class can be named FooTest. +// +// The user should put his test code between braces after using this +// macro. Example: +// +// TEST(FooTest, InitializesCorrectly) { +// Foo foo; +// EXPECT_TRUE(foo.StatusIsOK()); +// } + +#define TEST(test_case_name, test_name)\ + GTEST_TEST(test_case_name, test_name, ::testing::Test) + + +// Defines a test that uses a test fixture. +// +// The first parameter is the name of the test fixture class, which +// also doubles as the test case name. The second parameter is the +// name of the test within the test case. +// +// A test fixture class must be declared earlier. The user should put +// his test code between braces after using this macro. Example: +// +// class FooTest : public testing::Test { +// protected: +// virtual void SetUp() { b_.AddElement(3); } +// +// Foo a_; +// Foo b_; +// }; +// +// TEST_F(FooTest, InitializesCorrectly) { +// EXPECT_TRUE(a_.StatusIsOK()); +// } +// +// TEST_F(FooTest, ReturnsElementCountCorrectly) { +// EXPECT_EQ(0, a_.size()); +// EXPECT_EQ(1, b_.size()); +// } + +#define TEST_F(test_fixture, test_name)\ + GTEST_TEST(test_fixture, test_name, test_fixture) + +// Use this macro in main() to run all tests. It returns 0 if all +// tests are successful, or 1 otherwise. +// +// RUN_ALL_TESTS() should be invoked after the command line has been +// parsed by InitGoogleTest(). + +#define RUN_ALL_TESTS()\ + (::testing::UnitTest::GetInstance()->Run()) + +} // namespace testing + +#endif // GTEST_INCLUDE_GTEST_GTEST_H_ -- cgit v1.2.3 From e4e9a8bd7d2dbbad62030c8f80513e3c81b32213 Mon Sep 17 00:00:00 2001 From: shiqian Date: Tue, 8 Jul 2008 21:32:17 +0000 Subject: Makes the autotools scripts work on Mac OS X. Also hopefully makes gtest compile on Windows CE. --- include/gtest/gtest.h | 26 -------------------------- 1 file changed, 26 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 8e857c8d..2464f725 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -62,26 +62,11 @@ // Windows proper with Visual C++ and MS C library (_MSC_VER && !_WIN32_WCE) and // Windows Mobile with Visual C++ and no C library (_WIN32_WCE). -#if defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) -// When using Google Test on the Mac as a framework, all the includes -// will be in the framework headers folder along with gtest.h. Define -// GTEST_NOT_MAC_FRAMEWORK_MODE if you are building Google Test on the -// Mac and are not using it as a framework. More info on frameworks -// available here: -// http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/ -// Concepts/WhatAreFrameworks.html. -#include "gtest-death-test.h" // NOLINT -#include "gtest-internal.h" // NOLINT -#include "gtest-message.h" // NOLINT -#include "gtest-string.h" // NOLINT -#include "gtest_prod.h" // NOLINT -#else #include #include #include #include #include -#endif // defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) // Depending on the platform, different string classes are available. // On Windows, ::std::string compiles only when exceptions are @@ -964,18 +949,7 @@ class AssertHelper { // Includes the auto-generated header that implements a family of // generic predicate assertion macros. -#if defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) -// When using Google Test on the Mac as a framework, all the includes will be -// in the framework headers folder along with gtest.h. -// Define GTEST_NOT_MAC_FRAMEWORK_MODE if you are building Google Test on -// the Mac and are not using it as a framework. -// More info on frameworks available here: -// http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/ -// Concepts/WhatAreFrameworks.html. -#include "gtest_pred_impl.h" // NOLINT -#else #include -#endif // defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE) // Macros for testing equalities and inequalities. // -- cgit v1.2.3 From d5f13d4a257b6bfc43068f3a918989cf89af75ec Mon Sep 17 00:00:00 2001 From: shiqian Date: Wed, 6 Aug 2008 21:44:19 +0000 Subject: Changes test creation functions to factories. By Vlad Losev. --- include/gtest/gtest.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 2464f725..1f5d7640 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -310,11 +310,6 @@ class Test { }; -// Defines the type of a function pointer that creates a Test object -// when invoked. -typedef Test* (*TestMaker)(); - - // A TestInfo object stores the following information about a test: // // Test case name @@ -342,7 +337,9 @@ class TestInfo { // fixture_class_id: ID of the test fixture class // set_up_tc: pointer to the function that sets up the test case // tear_down_tc: pointer to the function that tears down the test case - // maker: pointer to the function that creates a test object + // factory: Pointer to the factory that creates a test object. + // The newly created TestInfo instance will assume + // ownershi pof the factory object. // // This is public only because it's needed by the TEST and TEST_F macros. // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. @@ -352,7 +349,7 @@ class TestInfo { internal::TypeId fixture_class_id, Test::SetUpTestCaseFunc set_up_tc, Test::TearDownTestCaseFunc tear_down_tc, - TestMaker maker); + internal::TestFactoryBase* factory); // Returns the test case name. const char* test_case_name() const; @@ -395,9 +392,11 @@ class TestInfo { internal::TestInfoImpl* impl() { return impl_; } const internal::TestInfoImpl* impl() const { return impl_; } - // Constructs a TestInfo object. + // Constructs a TestInfo object. The newly constructed instance assumes + // ownership of the factory object. TestInfo(const char* test_case_name, const char* name, - internal::TypeId fixture_class_id, TestMaker maker); + internal::TypeId fixture_class_id, + internal::TestFactoryBase* factory); // An opaque implementation object. internal::TestInfoImpl* impl_; -- cgit v1.2.3 From a2b1a8556ea64014606d78b09333d9c522430a25 Mon Sep 17 00:00:00 2001 From: shiqian Date: Mon, 8 Sep 2008 17:55:52 +0000 Subject: Adds support for type-parameterized tests (by Zhanyong Wan); also adds case-insensitive wide string comparison to the String class (by Vlad Losev). --- include/gtest/gtest.h | 83 +++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 45 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 1f5d7640..37ea6b04 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -62,11 +62,13 @@ // Windows proper with Visual C++ and MS C library (_MSC_VER && !_WIN32_WCE) and // Windows Mobile with Visual C++ and no C library (_WIN32_WCE). +#include #include #include #include #include #include +#include // Depending on the platform, different string classes are available. // On Windows, ::std::string compiles only when exceptions are @@ -217,12 +219,28 @@ class Test { // Defines types for pointers to functions that set up and tear down // a test case. - typedef void (*SetUpTestCaseFunc)(); - typedef void (*TearDownTestCaseFunc)(); + typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc; + typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc; // The d'tor is virtual as we intend to inherit from Test. virtual ~Test(); + // Sets up the stuff shared by all tests in this test case. + // + // Google Test will call Foo::SetUpTestCase() before running the first + // test in test case Foo. Hence a sub-class can define its own + // SetUpTestCase() method to shadow the one defined in the super + // class. + static void SetUpTestCase() {} + + // Tears down the stuff shared by all tests in this test case. + // + // Google Test will call Foo::TearDownTestCase() after running the last + // test in test case Foo. Hence a sub-class can define its own + // TearDownTestCase() method to shadow the one defined in the super + // class. + static void TearDownTestCase() {} + // Returns true iff the current test has a fatal failure. static bool HasFatalFailure(); @@ -245,22 +263,6 @@ class Test { // Creates a Test object. Test(); - // Sets up the stuff shared by all tests in this test case. - // - // Google Test will call Foo::SetUpTestCase() before running the first - // test in test case Foo. Hence a sub-class can define its own - // SetUpTestCase() method to shadow the one defined in the super - // class. - static void SetUpTestCase() {} - - // Tears down the stuff shared by all tests in this test case. - // - // Google Test will call Foo::TearDownTestCase() after running the last - // test in test case Foo. Hence a sub-class can define its own - // TearDownTestCase() method to shadow the one defined in the super - // class. - static void TearDownTestCase() {} - // Sets up the test fixture. virtual void SetUp(); @@ -327,36 +329,18 @@ class TestInfo { // don't inherit from TestInfo. ~TestInfo(); - // Creates a TestInfo object and registers it with the UnitTest - // singleton; returns the created object. - // - // Arguments: - // - // test_case_name: name of the test case - // name: name of the test - // fixture_class_id: ID of the test fixture class - // set_up_tc: pointer to the function that sets up the test case - // tear_down_tc: pointer to the function that tears down the test case - // factory: Pointer to the factory that creates a test object. - // The newly created TestInfo instance will assume - // ownershi pof the factory object. - // - // This is public only because it's needed by the TEST and TEST_F macros. - // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - static TestInfo* MakeAndRegisterInstance( - const char* test_case_name, - const char* name, - internal::TypeId fixture_class_id, - Test::SetUpTestCaseFunc set_up_tc, - Test::TearDownTestCaseFunc tear_down_tc, - internal::TestFactoryBase* factory); - // Returns the test case name. const char* test_case_name() const; // Returns the test name. const char* name() const; + // Returns the test case comment. + const char* test_case_comment() const; + + // Returns the test comment. + const char* comment() const; + // Returns true if this test should run. // // Google Test allows the user to filter the tests by their full names. @@ -383,6 +367,13 @@ class TestInfo { friend class internal::UnitTestImpl; friend class Test; friend class TestCase; + friend TestInfo* internal::MakeAndRegisterTestInfo( + const char* test_case_name, const char* name, + const char* test_case_comment, const char* comment, + internal::TypeId fixture_class_id, + Test::SetUpTestCaseFunc set_up_tc, + Test::TearDownTestCaseFunc tear_down_tc, + internal::TestFactoryBase* factory); // Increments the number of death tests encountered in this test so // far. @@ -395,6 +386,7 @@ class TestInfo { // Constructs a TestInfo object. The newly constructed instance assumes // ownership of the factory object. TestInfo(const char* test_case_name, const char* name, + const char* test_case_comment, const char* comment, internal::TypeId fixture_class_id, internal::TestFactoryBase* factory); @@ -1118,9 +1110,10 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, // // * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) // -// When expr unexpectedly fails or succeeds, Google Test prints the expected result -// and the actual result with both a human-readable string representation of -// the error, if available, as well as the hex result code. +// When expr unexpectedly fails or succeeds, Google Test prints the +// expected result and the actual result with both a human-readable +// string representation of the error, if available, as well as the +// hex result code. #define EXPECT_HRESULT_SUCCEEDED(expr) \ EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) -- cgit v1.2.3 From 019d19af978f05b774407e0d46a3bda2c18c67c6 Mon Sep 17 00:00:00 2001 From: shiqian Date: Fri, 12 Sep 2008 04:01:37 +0000 Subject: Improves thread-safe death tests by changing to the original working directory before they are executed; also fixes out-dated comments about death tests. --- include/gtest/gtest.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 37ea6b04..057efa26 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -479,6 +479,10 @@ class UnitTest { // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. int Run() GTEST_MUST_USE_RESULT; + // Returns the working directory when the first TEST() or TEST_F() + // was executed. The UnitTest object owns the string. + const char* original_working_dir() const; + // Returns the TestCase object for the test that's currently running, // or NULL if no test is running. const TestCase* current_test_case() const; -- cgit v1.2.3 From 36865d8d354465e3461eedf2949a4b7799985d5d Mon Sep 17 00:00:00 2001 From: shiqian Date: Fri, 12 Sep 2008 20:57:22 +0000 Subject: Adds exception assertions. By balaz.dan@gmail.com. --- include/gtest/gtest.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 057efa26..67cf4ac0 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -928,6 +928,28 @@ class AssertHelper { // Generates a success with a generic message. #define SUCCEED() GTEST_SUCCESS("Succeeded") +// Macros for testing exceptions. +// +// * {ASSERT|EXPECT}_THROW(statement, expected_exception): +// Tests that the statement throws the expected exception. +// * {ASSERT|EXPECT}_NO_THROW(statement): +// Tests that the statement doesn't throw any exception. +// * {ASSERT|EXPECT}_ANY_THROW(statement): +// Tests that the statement throws an exception. + +#define EXPECT_THROW(statement, expected_exception) \ + GTEST_TEST_THROW(statement, expected_exception, GTEST_NONFATAL_FAILURE) +#define EXPECT_NO_THROW(statement) \ + GTEST_TEST_NO_THROW(statement, GTEST_NONFATAL_FAILURE) +#define EXPECT_ANY_THROW(statement) \ + GTEST_TEST_ANY_THROW(statement, GTEST_NONFATAL_FAILURE) +#define ASSERT_THROW(statement, expected_exception) \ + GTEST_TEST_THROW(statement, expected_exception, GTEST_FATAL_FAILURE) +#define ASSERT_NO_THROW(statement) \ + GTEST_TEST_NO_THROW(statement, GTEST_FATAL_FAILURE) +#define ASSERT_ANY_THROW(statement) \ + GTEST_TEST_ANY_THROW(statement, GTEST_FATAL_FAILURE) + // Boolean assertions. #define EXPECT_TRUE(condition) \ GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ -- cgit v1.2.3 From e0865dd9199e8fffd5c2f95a68de6c1851f77c15 Mon Sep 17 00:00:00 2001 From: shiqian Date: Sat, 11 Oct 2008 07:20:02 +0000 Subject: Many changes: - appends "_" to internal macro names (by Markus Heule). - makes Google Test work with newer versions of tools on Symbian and Windows CE (by Mika Raento). - adds the (ASSERT|EXPECT)_NO_FATAL_FAILURE macros (by Markus Heule). - changes EXPECT_(NON|)FATAL_FAILURE to catch failures in the current thread only (by Markus Heule). - adds the EXPECT_(NON|)FATAL_FAILURE_ON_ALL_THREADS macros (by Markus Heule). - adds GTEST_HAS_PTHREAD and GTEST_IS_THREADSAFE to indicate the availability of and Google Test's thread-safety (by Zhanyong Wan). - adds scons/SConscript for building with scons (by Joi Sigurdsson). - adds src/gtest-all.cc for building Google Test from a single file (by Markus Heule). - updates the xcode project to include new tests (by Preston Jackson). --- include/gtest/gtest.h | 98 +++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 46 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 67cf4ac0..8df25aba 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -53,7 +53,6 @@ // The following platform macros are used throughout Google Test: // _WIN32_WCE Windows CE (set in project files) -// __SYMBIAN32__ Symbian (set by Symbian tool chain) // // Note that even though _MSC_VER and _WIN32_WCE really indicate a compiler // and a Win32 implementation, respectively, we use them to indicate the @@ -68,6 +67,7 @@ #include #include #include +#include #include // Depending on the platform, different string classes are available. @@ -97,19 +97,11 @@ const int kMaxStackTraceDepth = 100; // This flag specifies the maximum number of stack frames to be // printed in a failure message. -GTEST_DECLARE_int32(stack_trace_depth); +GTEST_DECLARE_int32_(stack_trace_depth); // This flag controls whether Google Test includes Google Test internal // stack frames in failure stack traces. -GTEST_DECLARE_bool(show_internal_stack_frames); - -// The possible outcomes of a test part (i.e. an assertion or an -// explicit SUCCEED(), FAIL(), or ADD_FAILURE()). -enum TestPartResultType { - TPRT_SUCCESS, // Succeeded. - TPRT_NONFATAL_FAILURE, // Failed but the test can continue. - TPRT_FATAL_FAILURE // Failed and the test should be terminated. -}; +GTEST_DECLARE_bool_(show_internal_stack_frames); namespace internal { @@ -308,7 +300,7 @@ class Test { virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } // We disallow copying Tests. - GTEST_DISALLOW_COPY_AND_ASSIGN(Test); + GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); }; @@ -393,7 +385,7 @@ class TestInfo { // An opaque implementation object. internal::TestInfoImpl* impl_; - GTEST_DISALLOW_COPY_AND_ASSIGN(TestInfo); + GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); }; // An Environment object is capable of setting up and tearing down an @@ -477,7 +469,7 @@ class UnitTest { // This method can only be called from the main thread. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - int Run() GTEST_MUST_USE_RESULT; + int Run() GTEST_MUST_USE_RESULT_; // Returns the working directory when the first TEST() or TEST_F() // was executed. The UnitTest object owns the string. @@ -523,7 +515,7 @@ class UnitTest { internal::UnitTestImpl* impl_; // We disallow copying UnitTest. - GTEST_DISALLOW_COPY_AND_ASSIGN(UnitTest); + GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest); }; // A convenient wrapper for adding an environment for the test @@ -707,7 +699,7 @@ class EqHelper { // with gcc 4. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -#define GTEST_IMPL_CMP_HELPER(op_name, op)\ +#define GTEST_IMPL_CMP_HELPER_(op_name, op)\ template \ AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ const T1& val1, const T2& val2) {\ @@ -727,17 +719,17 @@ AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. // Implements the helper function for {ASSERT|EXPECT}_NE -GTEST_IMPL_CMP_HELPER(NE, !=) +GTEST_IMPL_CMP_HELPER_(NE, !=) // Implements the helper function for {ASSERT|EXPECT}_LE -GTEST_IMPL_CMP_HELPER(LE, <=) +GTEST_IMPL_CMP_HELPER_(LE, <=) // Implements the helper function for {ASSERT|EXPECT}_LT -GTEST_IMPL_CMP_HELPER(LT, < ) +GTEST_IMPL_CMP_HELPER_(LT, < ) // Implements the helper function for {ASSERT|EXPECT}_GE -GTEST_IMPL_CMP_HELPER(GE, >=) +GTEST_IMPL_CMP_HELPER_(GE, >=) // Implements the helper function for {ASSERT|EXPECT}_GT -GTEST_IMPL_CMP_HELPER(GT, > ) +GTEST_IMPL_CMP_HELPER_(GT, > ) -#undef GTEST_IMPL_CMP_HELPER +#undef GTEST_IMPL_CMP_HELPER_ // The helper function for {ASSERT|EXPECT}_STREQ. // @@ -881,7 +873,7 @@ class AssertHelper { AssertHelper(TestPartResultType type, const char* file, int line, const char* message); // Message assignment is a semantic trick to enable assertion - // streaming; see the GTEST_MESSAGE macro below. + // streaming; see the GTEST_MESSAGE_ macro below. void operator=(const Message& message) const; private: TestPartResultType const type_; @@ -889,7 +881,7 @@ class AssertHelper { int const line_; String const message_; - GTEST_DISALLOW_COPY_AND_ASSIGN(AssertHelper); + GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); }; } // namespace internal @@ -920,13 +912,13 @@ class AssertHelper { // << "There are still pending requests " << "on port " << port; // Generates a nonfatal failure with a generic message. -#define ADD_FAILURE() GTEST_NONFATAL_FAILURE("Failed") +#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") // Generates a fatal failure with a generic message. -#define FAIL() GTEST_FATAL_FAILURE("Failed") +#define FAIL() GTEST_FATAL_FAILURE_("Failed") // Generates a success with a generic message. -#define SUCCEED() GTEST_SUCCESS("Succeeded") +#define SUCCEED() GTEST_SUCCESS_("Succeeded") // Macros for testing exceptions. // @@ -938,31 +930,31 @@ class AssertHelper { // Tests that the statement throws an exception. #define EXPECT_THROW(statement, expected_exception) \ - GTEST_TEST_THROW(statement, expected_exception, GTEST_NONFATAL_FAILURE) + GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) #define EXPECT_NO_THROW(statement) \ - GTEST_TEST_NO_THROW(statement, GTEST_NONFATAL_FAILURE) + GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) #define EXPECT_ANY_THROW(statement) \ - GTEST_TEST_ANY_THROW(statement, GTEST_NONFATAL_FAILURE) + GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) #define ASSERT_THROW(statement, expected_exception) \ - GTEST_TEST_THROW(statement, expected_exception, GTEST_FATAL_FAILURE) + GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) #define ASSERT_NO_THROW(statement) \ - GTEST_TEST_NO_THROW(statement, GTEST_FATAL_FAILURE) + GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) #define ASSERT_ANY_THROW(statement) \ - GTEST_TEST_ANY_THROW(statement, GTEST_FATAL_FAILURE) + GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) // Boolean assertions. #define EXPECT_TRUE(condition) \ - GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ - GTEST_NONFATAL_FAILURE) + GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ + GTEST_NONFATAL_FAILURE_) #define EXPECT_FALSE(condition) \ - GTEST_TEST_BOOLEAN(!(condition), #condition, true, false, \ - GTEST_NONFATAL_FAILURE) + GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ + GTEST_NONFATAL_FAILURE_) #define ASSERT_TRUE(condition) \ - GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ - GTEST_FATAL_FAILURE) + GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ + GTEST_FATAL_FAILURE_) #define ASSERT_FALSE(condition) \ - GTEST_TEST_BOOLEAN(!(condition), #condition, true, false, \ - GTEST_FATAL_FAILURE) + GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ + GTEST_FATAL_FAILURE_) // Includes the auto-generated header that implements a family of // generic predicate assertion macros. @@ -1016,7 +1008,7 @@ class AssertHelper { #define EXPECT_EQ(expected, actual) \ EXPECT_PRED_FORMAT2(::testing::internal:: \ - EqHelper::Compare, \ + EqHelper::Compare, \ expected, actual) #define EXPECT_NE(expected, actual) \ EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual) @@ -1031,7 +1023,7 @@ class AssertHelper { #define ASSERT_EQ(expected, actual) \ ASSERT_PRED_FORMAT2(::testing::internal:: \ - EqHelper::Compare, \ + EqHelper::Compare, \ expected, actual) #define ASSERT_NE(val1, val2) \ ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) @@ -1154,6 +1146,20 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, #endif // GTEST_OS_WINDOWS +// Macros that execute statement and check that it doesn't generate new fatal +// failures in the current thread. +// +// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); +// +// Examples: +// +// EXPECT_NO_FATAL_FAILURE(Process()); +// ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; +// +#define ASSERT_NO_FATAL_FAILURE(statement) \ + GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) +#define EXPECT_NO_FATAL_FAILURE(statement) \ + GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) // Causes a trace (including the source file path, the current line // number, and the given message) to be included in every test failure @@ -1167,7 +1173,7 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, // to appear in the same block - as long as they are on different // lines. #define SCOPED_TRACE(message) \ - ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN(gtest_trace_, __LINE__)(\ + ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ __FILE__, __LINE__, ::testing::Message() << (message)) @@ -1188,7 +1194,7 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, // } #define TEST(test_case_name, test_name)\ - GTEST_TEST(test_case_name, test_name, ::testing::Test) + GTEST_TEST_(test_case_name, test_name, ::testing::Test) // Defines a test that uses a test fixture. @@ -1218,7 +1224,7 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, // } #define TEST_F(test_fixture, test_name)\ - GTEST_TEST(test_fixture, test_name, test_fixture) + GTEST_TEST_(test_fixture, test_name, test_fixture) // Use this macro in main() to run all tests. It returns 0 if all // tests are successful, or 1 otherwise. -- cgit v1.2.3 From 3d7042176307f0d7700a3640f3b3bcc8790b8fcd Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 20 Nov 2008 01:40:35 +0000 Subject: Value-parameterized tests and many bugfixes --- include/gtest/gtest.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 8df25aba..dae26473 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -66,6 +66,7 @@ #include #include #include +#include #include #include #include @@ -483,6 +484,12 @@ class UnitTest { // or NULL if no test is running. const TestInfo* current_test_info() const; +#ifdef GTEST_HAS_PARAM_TEST + // Returns the ParameterizedTestCaseRegistry object used to keep track of + // value-parameterized tests and instantiate and register them. + internal::ParameterizedTestCaseRegistry& parameterized_test_registry(); +#endif // GTEST_HAS_PARAM_TEST + // Accessors for the implementation object. internal::UnitTestImpl* impl() { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; } @@ -886,6 +893,65 @@ class AssertHelper { } // namespace internal +#ifdef GTEST_HAS_PARAM_TEST +// The abstract base class that all value-parameterized tests inherit from. +// +// This class adds support for accessing the test parameter value via +// the GetParam() method. +// +// Use it with one of the parameter generator defining functions, like Range(), +// Values(), ValuesIn(), Bool(), and Combine(). +// +// class FooTest : public ::testing::TestWithParam { +// protected: +// FooTest() { +// // Can use GetParam() here. +// } +// virtual ~FooTest() { +// // Can use GetParam() here. +// } +// virtual void SetUp() { +// // Can use GetParam() here. +// } +// virtual void TearDown { +// // Can use GetParam() here. +// } +// }; +// TEST_P(FooTest, DoesBar) { +// // Can use GetParam() method here. +// Foo foo; +// ASSERT_TRUE(foo.DoesBar(GetParam())); +// } +// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); + +template +class TestWithParam : public Test { + public: + typedef T ParamType; + + // The current parameter value. Is also available in the test fixture's + // constructor. + const ParamType& GetParam() const { return *parameter_; } + + private: + // Sets parameter value. The caller is responsible for making sure the value + // remains alive and unchanged throughout the current test. + static void SetParam(const ParamType* parameter) { + parameter_ = parameter; + } + + // Static value used for accessing parameter during a test lifetime. + static const ParamType* parameter_; + + // TestClass must be a subclass of TestWithParam. + template friend class internal::ParameterizedTestFactory; +}; + +template +const T* TestWithParam::parameter_ = NULL; + +#endif // GTEST_HAS_PARAM_TEST + // Macros for indicating success/failure in test code. // ADD_FAILURE unconditionally adds a failure to the current test. -- cgit v1.2.3 From c440a6923aa65d5be64134a6f430a5867a63df3f Mon Sep 17 00:00:00 2001 From: shiqian Date: Mon, 24 Nov 2008 20:13:22 +0000 Subject: Enables the Python tests to run with 2.3 (necessary for testing on Mac OS X Tiger); also fixes gtest_output_test when built with xcode. --- include/gtest/gtest.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index dae26473..38fcd7d8 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1259,8 +1259,18 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, // EXPECT_TRUE(foo.StatusIsOK()); // } +// Note that we call GetTestTypeId() instead of GetTypeId< +// ::testing::Test>() here to get the type ID of testing::Test. This +// is to work around a suspected linker bug when using Google Test as +// a framework on Mac OS X. The bug causes GetTypeId< +// ::testing::Test>() to return different values depending on whether +// the call is from the Google Test framework itself or from user test +// code. GetTestTypeId() is guaranteed to always return the same +// value, as it always calls GetTypeId<>() from the Google Test +// framework. #define TEST(test_case_name, test_name)\ - GTEST_TEST_(test_case_name, test_name, ::testing::Test) + GTEST_TEST_(test_case_name, test_name,\ + ::testing::Test, ::testing::internal::GetTestTypeId()) // Defines a test that uses a test fixture. @@ -1290,7 +1300,8 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, // } #define TEST_F(test_fixture, test_name)\ - GTEST_TEST_(test_fixture, test_name, test_fixture) + GTEST_TEST_(test_fixture, test_name, test_fixture,\ + ::testing::internal::GetTypeId()) // Use this macro in main() to run all tests. It returns 0 if all // tests are successful, or 1 otherwise. -- cgit v1.2.3 From 1998cf5d32a19aaffe8652545802744d9133022d Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 26 Nov 2008 20:48:45 +0000 Subject: Allow Google Mock to initialize Google Test --- include/gtest/gtest.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 38fcd7d8..ebd3123b 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -554,13 +554,13 @@ inline Environment* AddGlobalTestEnvironment(Environment* env) { // // No value is returned. Instead, the Google Test flag variables are // updated. +// +// Calling the function for the second time has no user-visible effect. void InitGoogleTest(int* argc, char** argv); // This overloaded version can be used in Windows programs compiled in // UNICODE mode. -#ifdef GTEST_OS_WINDOWS void InitGoogleTest(int* argc, wchar_t** argv); -#endif // GTEST_OS_WINDOWS namespace internal { -- cgit v1.2.3 From 53e0dc4041f660b6517b15b08b496e164be614f1 Mon Sep 17 00:00:00 2001 From: shiqian Date: Thu, 8 Jan 2009 01:10:31 +0000 Subject: Implements the --gtest_death_test_use_fork flag and StaticAssertTypeEq. --- include/gtest/gtest.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index ebd3123b..dfa338b2 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1242,6 +1242,52 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ __FILE__, __LINE__, ::testing::Message() << (message)) +namespace internal { + +// This template is declared, but intentionally undefined. +template +struct StaticAssertTypeEqHelper; + +template +struct StaticAssertTypeEqHelper {}; + +} // namespace internal + +// Compile-time assertion for type equality. +// StaticAssertTypeEq() compiles iff type1 and type2 are +// the same type. The value it returns is not interesting. +// +// Instead of making StaticAssertTypeEq a class template, we make it a +// function template that invokes a helper class template. This +// prevents a user from misusing StaticAssertTypeEq by +// defining objects of that type. +// +// CAVEAT: +// +// When used inside a method of a class template, +// StaticAssertTypeEq() is effective ONLY IF the method is +// instantiated. For example, given: +// +// template class Foo { +// public: +// void Bar() { testing::StaticAssertTypeEq(); } +// }; +// +// the code: +// +// void Test1() { Foo foo; } +// +// will NOT generate a compiler error, as Foo::Bar() is never +// actually instantiated. Instead, you need: +// +// void Test2() { Foo foo; foo.Bar(); } +// +// to cause a compiler error. +template +bool StaticAssertTypeEq() { + internal::StaticAssertTypeEqHelper(); + return true; +} // Defines a test. // -- cgit v1.2.3 From 4b83461e9772cce62e84310060fe84172e9bf4ba Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 29 Jan 2009 06:49:00 +0000 Subject: Fixes some warnings when compiled with MSVC at warning level 4. --- include/gtest/gtest.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index dfa338b2..f1184dee 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -614,10 +614,20 @@ AssertionResult CmpHelperEQ(const char* expected_expression, const char* actual_expression, const T1& expected, const T2& actual) { +#ifdef _MSC_VER +#pragma warning(push) // Saves the current warning state. +#pragma warning(disable:4389) // Temporarily disables warning on + // signed/unsigned mismatch. +#endif + if (expected == actual) { return AssertionSuccess(); } +#ifdef _MSC_VER +#pragma warning(pop) // Restores the warning state. +#endif + return EqFailure(expected_expression, actual_expression, FormatForComparisonFailureMessage(expected, actual), @@ -688,7 +698,7 @@ class EqHelper { template static AssertionResult Compare(const char* expected_expression, const char* actual_expression, - const T1& expected, + const T1& /* expected */, T2* actual) { // We already know that 'expected' is a null pointer. return CmpHelperEQ(expected_expression, actual_expression, @@ -1315,7 +1325,7 @@ bool StaticAssertTypeEq() { // value, as it always calls GetTypeId<>() from the Google Test // framework. #define TEST(test_case_name, test_name)\ - GTEST_TEST_(test_case_name, test_name,\ + GTEST_TEST_(test_case_name, test_name, \ ::testing::Test, ::testing::internal::GetTestTypeId()) @@ -1346,7 +1356,7 @@ bool StaticAssertTypeEq() { // } #define TEST_F(test_fixture, test_name)\ - GTEST_TEST_(test_fixture, test_name, test_fixture,\ + GTEST_TEST_(test_fixture, test_name, test_fixture, \ ::testing::internal::GetTypeId()) // Use this macro in main() to run all tests. It returns 0 if all -- cgit v1.2.3 From ad99ca14461f0e929d835d29518e11c05e8d41f0 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 2 Feb 2009 06:37:03 +0000 Subject: Exposes gtest flags to user code access. By Alexander Demin. --- include/gtest/gtest.h | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index f1184dee..f28757de 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -93,17 +93,54 @@ namespace testing { -// The upper limit for valid stack trace depths. -const int kMaxStackTraceDepth = 100; +// Declares the flags. -// This flag specifies the maximum number of stack frames to be -// printed in a failure message. -GTEST_DECLARE_int32_(stack_trace_depth); +// This flag temporary enables the disabled tests. +GTEST_DECLARE_bool_(also_run_disabled_tests); + +// This flag brings the debugger on an assertion failure. +GTEST_DECLARE_bool_(break_on_failure); + +// This flag controls whether Google Test catches all test-thrown exceptions +// and logs them as failures. +GTEST_DECLARE_bool_(catch_exceptions); + +// This flag enables using colors in terminal output. Available values are +// "yes" to enable colors, "no" (disable colors), or "auto" (the default) +// to let Google Test decide. +GTEST_DECLARE_string_(color); + +// This flag sets up the filter to select by name using a glob pattern +// the tests to run. If the filter is not given all tests are executed. +GTEST_DECLARE_string_(filter); + +// This flag causes the Google Test to list tests. None of the tests listed +// are actually run if the flag is provided. +GTEST_DECLARE_bool_(list_tests); + +// This flag controls whether Google Test emits a detailed XML report to a file +// in addition to its normal textual output. +GTEST_DECLARE_string_(output); + +// This flags control whether Google Test prints the elapsed time for each +// test. +GTEST_DECLARE_bool_(print_time); + +// This flag sets how many times the tests are repeated. The default value +// is 1. If the value is -1 the tests are repeating forever. +GTEST_DECLARE_int32_(repeat); // This flag controls whether Google Test includes Google Test internal // stack frames in failure stack traces. GTEST_DECLARE_bool_(show_internal_stack_frames); +// This flag specifies the maximum number of stack frames to be +// printed in a failure message. +GTEST_DECLARE_int32_(stack_trace_depth); + +// The upper limit for valid stack trace depths. +const int kMaxStackTraceDepth = 100; + namespace internal { class GTestFlagSaver; -- cgit v1.2.3 From 0af0709b02899f9177db55eba7929e65e5834b29 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 23 Feb 2009 23:21:55 +0000 Subject: Cleans up macro definitions. --- include/gtest/gtest.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index f28757de..7fecb92f 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -390,7 +390,7 @@ class TestInfo { // Returns the result of the test. const internal::TestResult* result() const; private: -#ifdef GTEST_HAS_DEATH_TEST +#if GTEST_HAS_DEATH_TEST friend class internal::DefaultDeathTestFactory; #endif // GTEST_HAS_DEATH_TEST friend class internal::TestInfoImpl; @@ -521,7 +521,7 @@ class UnitTest { // or NULL if no test is running. const TestInfo* current_test_info() const; -#ifdef GTEST_HAS_PARAM_TEST +#if GTEST_HAS_PARAM_TEST // Returns the ParameterizedTestCaseRegistry object used to keep track of // value-parameterized tests and instantiate and register them. internal::ParameterizedTestCaseRegistry& parameterized_test_registry(); @@ -940,7 +940,7 @@ class AssertHelper { } // namespace internal -#ifdef GTEST_HAS_PARAM_TEST +#if GTEST_HAS_PARAM_TEST // The abstract base class that all value-parameterized tests inherit from. // // This class adds support for accessing the test parameter value via @@ -1234,7 +1234,7 @@ AssertionResult DoubleLE(const char* expr1, const char* expr2, double val1, double val2); -#ifdef GTEST_OS_WINDOWS +#if GTEST_OS_WINDOWS // Macros that test for HRESULT failure and success, these are only useful // on Windows, and rely on Windows SDK macros and APIs to compile. -- cgit v1.2.3 From 40e72a8a837b47cbfe2e695068c1845073ab2630 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 6 Mar 2009 20:05:23 +0000 Subject: Implements --gtest_throw_on_failure for using gtest with other testing frameworks. --- include/gtest/gtest.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 7fecb92f..9b72b636 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -101,20 +101,20 @@ GTEST_DECLARE_bool_(also_run_disabled_tests); // This flag brings the debugger on an assertion failure. GTEST_DECLARE_bool_(break_on_failure); -// This flag controls whether Google Test catches all test-thrown exceptions -// and logs them as failures. +// This flag controls whether Google Test catches all test-thrown exceptions +// and logs them as failures. GTEST_DECLARE_bool_(catch_exceptions); -// This flag enables using colors in terminal output. Available values are -// "yes" to enable colors, "no" (disable colors), or "auto" (the default) +// This flag enables using colors in terminal output. Available values are +// "yes" to enable colors, "no" (disable colors), or "auto" (the default) // to let Google Test decide. GTEST_DECLARE_string_(color); -// This flag sets up the filter to select by name using a glob pattern +// This flag sets up the filter to select by name using a glob pattern // the tests to run. If the filter is not given all tests are executed. GTEST_DECLARE_string_(filter); -// This flag causes the Google Test to list tests. None of the tests listed +// This flag causes the Google Test to list tests. None of the tests listed // are actually run if the flag is provided. GTEST_DECLARE_bool_(list_tests); @@ -122,11 +122,11 @@ GTEST_DECLARE_bool_(list_tests); // in addition to its normal textual output. GTEST_DECLARE_string_(output); -// This flags control whether Google Test prints the elapsed time for each +// This flags control whether Google Test prints the elapsed time for each // test. GTEST_DECLARE_bool_(print_time); -// This flag sets how many times the tests are repeated. The default value +// This flag sets how many times the tests are repeated. The default value // is 1. If the value is -1 the tests are repeating forever. GTEST_DECLARE_int32_(repeat); @@ -138,6 +138,11 @@ GTEST_DECLARE_bool_(show_internal_stack_frames); // printed in a failure message. GTEST_DECLARE_int32_(stack_trace_depth); +// When this flag is specified, a failed assertion will throw an +// exception if exceptions are enabled, or exit the program with a +// non-zero code otherwise. +GTEST_DECLARE_bool_(throw_on_failure); + // The upper limit for valid stack trace depths. const int kMaxStackTraceDepth = 100; -- cgit v1.2.3 From 6a26383e31cf79dd0acf89bf3a53c7a805decf1d Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 31 Mar 2009 16:27:55 +0000 Subject: Cleans up the use of GTEST_OS_WINDOWS and _MSC_VER. --- include/gtest/gtest.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 9b72b636..6d87e035 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -51,15 +51,8 @@ #ifndef GTEST_INCLUDE_GTEST_GTEST_H_ #define GTEST_INCLUDE_GTEST_GTEST_H_ -// The following platform macros are used throughout Google Test: +// The following platform macro is used throughout Google Test: // _WIN32_WCE Windows CE (set in project files) -// -// Note that even though _MSC_VER and _WIN32_WCE really indicate a compiler -// and a Win32 implementation, respectively, we use them to indicate the -// combination of compiler - Win 32 API - C library, since the code currently -// only supports: -// Windows proper with Visual C++ and MS C library (_MSC_VER && !_WIN32_WCE) and -// Windows Mobile with Visual C++ and no C library (_WIN32_WCE). #include #include -- cgit v1.2.3 From c12f63214e9b7761d27e68353e4aaf1761c9cf88 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 7 Apr 2009 21:03:22 +0000 Subject: Adds sample4_unittest to scons (by Vlad Losev); adds logic for getting the thread count on Mac (by Vlad Losev); adds HasFailure() and HasNonfatalFailure() (by Zhanyong Wan). --- include/gtest/gtest.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 6d87e035..26d76b26 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -272,6 +272,13 @@ class Test { // Returns true iff the current test has a fatal failure. static bool HasFatalFailure(); + // Returns true iff the current test has a non-fatal failure. + static bool HasNonfatalFailure(); + + // Returns true iff the current test has a (either fatal or + // non-fatal) failure. + static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } + // Logs a property for the current test. Only the last value for a given // key is remembered. // These are public static so they can be called from utility functions -- cgit v1.2.3 From fa2b06c52fa3ffb1909ed8b928e106292609cfcb Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 24 Apr 2009 20:27:29 +0000 Subject: Makes --gtest_list_tests honor the test filter (by Jay Campan). --- include/gtest/gtest.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 26d76b26..f5437784 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -376,7 +376,12 @@ class TestInfo { // Returns the test comment. const char* comment() const; - // Returns true if this test should run. + // Returns true if this test matches the user-specified filter. + bool matches_filter() const; + + // Returns true if this test should run, that is if the test is not disabled + // (or it is disabled but the also_run_disabled_tests flag has been specified) + // and its full name matches the user-specified filter. // // Google Test allows the user to filter the tests by their full names. // The full name of a test Bar in test case Foo is defined as -- cgit v1.2.3 From ae3247986bbbafcc913b5fe6132090ad6f1c3f36 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 19 Jun 2009 00:24:28 +0000 Subject: Fixes broken gtest_unittest on Cygwin and cleans it up (by Vlad Losev); fixes the wrong usage of os.environ.clear() in gtest_output_test.py (by Vlad Losev); fixes the logic for detecting Symbian (by Zhanyong Wan); moves TestProperty for event listener (by Vlad Losev). --- include/gtest/gtest.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index f5437784..d15909b1 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -346,6 +346,44 @@ class Test { GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); }; +namespace internal { + +// A copyable object representing a user specified test property which can be +// output as a key/value string pair. +// +// Don't inherit from TestProperty as its destructor is not virtual. +class TestProperty { + public: + // C'tor. TestProperty does NOT have a default constructor. + // Always use this constructor (with parameters) to create a + // TestProperty object. + TestProperty(const char* key, const char* value) : + key_(key), value_(value) { + } + + // Gets the user supplied key. + const char* key() const { + return key_.c_str(); + } + + // Gets the user supplied value. + const char* value() const { + return value_.c_str(); + } + + // Sets a new value, overriding the one supplied in the constructor. + void SetValue(const char* new_value) { + value_ = new_value; + } + + private: + // The key supplied by the user. + String key_; + // The value supplied by the user. + String value_; +}; + +} // namespace internal // A TestInfo object stores the following information about a test: // -- cgit v1.2.3 From 3c181b5657a51d73166c1012dad80ed3ee7a30ee Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 19 Jun 2009 21:20:40 +0000 Subject: Moves TestResult from gtest-internal-inl.h to gtest.h to prepare for the even listener API work (by Vlad Losev); cleans up the scons script (by Zhanyong Wan). --- include/gtest/gtest.h | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index d15909b1..1c504f8a 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -383,6 +383,102 @@ class TestProperty { String value_; }; +// The result of a single Test. This includes a list of +// TestPartResults, a list of TestProperties, a count of how many +// death tests there are in the Test, and how much time it took to run +// the Test. +// +// TestResult is not copyable. +class TestResult { + public: + // Creates an empty TestResult. + TestResult(); + + // D'tor. Do not inherit from TestResult. + ~TestResult(); + + // Gets the list of TestPartResults. + const internal::List& test_part_results() const { + return *test_part_results_; + } + + // Gets the list of TestProperties. + const internal::List& test_properties() const { + return *test_properties_; + } + + // Gets the number of successful test parts. + int successful_part_count() const; + + // Gets the number of failed test parts. + int failed_part_count() const; + + // Gets the number of all test parts. This is the sum of the number + // of successful test parts and the number of failed test parts. + int total_part_count() const; + + // Returns true iff the test passed (i.e. no test part failed). + bool Passed() const { return !Failed(); } + + // Returns true iff the test failed. + bool Failed() const { return failed_part_count() > 0; } + + // Returns true iff the test fatally failed. + bool HasFatalFailure() const; + + // Returns true iff the test has a non-fatal failure. + bool HasNonfatalFailure() const; + + // Returns the elapsed time, in milliseconds. + TimeInMillis elapsed_time() const { return elapsed_time_; } + + // Sets the elapsed time. + void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } + + // Adds a test part result to the list. + void AddTestPartResult(const TestPartResult& test_part_result); + + // Adds a test property to the list. The property is validated and may add + // a non-fatal failure if invalid (e.g., if it conflicts with reserved + // key names). If a property is already recorded for the same key, the + // value will be updated, rather than storing multiple values for the same + // key. + void RecordProperty(const internal::TestProperty& test_property); + + // Adds a failure if the key is a reserved attribute of Google Test + // testcase tags. Returns true if the property is valid. + // TODO(russr): Validate attribute names are legal and human readable. + static bool ValidateTestProperty(const internal::TestProperty& test_property); + + // Returns the death test count. + int death_test_count() const { return death_test_count_; } + + // Increments the death test count, returning the new count. + int increment_death_test_count() { return ++death_test_count_; } + + // Clears the test part results. + void ClearTestPartResults(); + + // Clears the object. + void Clear(); + private: + // Protects mutable state of the property list and of owned properties, whose + // values may be updated. + internal::Mutex test_properites_mutex_; + + // The list of TestPartResults + scoped_ptr > test_part_results_; + // The list of TestProperties + scoped_ptr > test_properties_; + // Running count of death tests. + int death_test_count_; + // The elapsed time, in milliseconds. + TimeInMillis elapsed_time_; + + // We disallow copying TestResult. + GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); +}; // class TestResult + } // namespace internal // A TestInfo object stores the following information about a test: -- cgit v1.2.3 From ef29ce3576240e51f289e49b2c4e156b414d6685 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 22 Jun 2009 23:29:24 +0000 Subject: Turns on exceptions when compiling gtest_output_test (by Vlad Losev); moves TestCase to gtest.h to prepare for the event listener API (by Vlad Losev). --- include/gtest/gtest.h | 129 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 1c504f8a..66653d1c 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -142,6 +142,7 @@ const int kMaxStackTraceDepth = 100; namespace internal { class GTestFlagSaver; +class TestCase; // A collection of related tests. // Converts a streamable value to a String. A NULL pointer is // converted to "(null)". When the input value is a ::string, @@ -540,7 +541,7 @@ class TestInfo { friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; friend class Test; - friend class TestCase; + friend class internal::TestCase; friend TestInfo* internal::MakeAndRegisterTestInfo( const char* test_case_name, const char* name, const char* test_case_comment, const char* comment, @@ -570,6 +571,130 @@ class TestInfo { GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); }; +namespace internal { + +// A test case, which consists of a list of TestInfos. +// +// TestCase is not copyable. +class TestCase { + public: + // Creates a TestCase with the given name. + // + // TestCase does NOT have a default constructor. Always use this + // constructor to create a TestCase object. + // + // Arguments: + // + // name: name of the test case + // set_up_tc: pointer to the function that sets up the test case + // tear_down_tc: pointer to the function that tears down the test case + TestCase(const char* name, const char* comment, + Test::SetUpTestCaseFunc set_up_tc, + Test::TearDownTestCaseFunc tear_down_tc); + + // Destructor of TestCase. + virtual ~TestCase(); + + // Gets the name of the TestCase. + const char* name() const { return name_.c_str(); } + + // Returns the test case comment. + const char* comment() const { return comment_.c_str(); } + + // Returns true if any test in this test case should run. + bool should_run() const { return should_run_; } + + // Sets the should_run member. + void set_should_run(bool should) { should_run_ = should; } + + // Gets the (mutable) list of TestInfos in this TestCase. + internal::List& test_info_list() { return *test_info_list_; } + + // Gets the (immutable) list of TestInfos in this TestCase. + const internal::List & test_info_list() const { + return *test_info_list_; + } + + // Gets the number of successful tests in this test case. + int successful_test_count() const; + + // Gets the number of failed tests in this test case. + int failed_test_count() const; + + // Gets the number of disabled tests in this test case. + int disabled_test_count() const; + + // Get the number of tests in this test case that should run. + int test_to_run_count() const; + + // Gets the number of all tests in this test case. + int total_test_count() const; + + // Returns true iff the test case passed. + bool Passed() const { return !Failed(); } + + // Returns true iff the test case failed. + bool Failed() const { return failed_test_count() > 0; } + + // Returns the elapsed time, in milliseconds. + internal::TimeInMillis elapsed_time() const { return elapsed_time_; } + + // Adds a TestInfo to this test case. Will delete the TestInfo upon + // destruction of the TestCase object. + void AddTestInfo(TestInfo * test_info); + + // Finds and returns a TestInfo with the given name. If one doesn't + // exist, returns NULL. + TestInfo* GetTestInfo(const char* test_name); + + // Clears the results of all tests in this test case. + void ClearResult(); + + // Clears the results of all tests in the given test case. + static void ClearTestCaseResult(TestCase* test_case) { + test_case->ClearResult(); + } + + // Runs every test in this TestCase. + void Run(); + + // Runs every test in the given TestCase. + static void RunTestCase(TestCase * test_case) { test_case->Run(); } + + // Returns true iff test passed. + static bool TestPassed(const TestInfo * test_info); + + // Returns true iff test failed. + static bool TestFailed(const TestInfo * test_info); + + // Returns true iff test is disabled. + static bool TestDisabled(const TestInfo * test_info); + + // Returns true if the given test should run. + static bool ShouldRunTest(const TestInfo *test_info); + + private: + // Name of the test case. + internal::String name_; + // Comment on the test case. + internal::String comment_; + // List of TestInfos. + internal::List* test_info_list_; + // Pointer to the function that sets up the test case. + Test::SetUpTestCaseFunc set_up_tc_; + // Pointer to the function that tears down the test case. + Test::TearDownTestCaseFunc tear_down_tc_; + // True iff any test in this test case should run. + bool should_run_; + // Elapsed time, in milliseconds. + internal::TimeInMillis elapsed_time_; + + // We disallow copying TestCases. + GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); +}; + +} // namespace internal + // An Environment object is capable of setting up and tearing down an // environment. The user should subclass this to define his own // environment(s). @@ -659,7 +784,7 @@ class UnitTest { // Returns the TestCase object for the test that's currently running, // or NULL if no test is running. - const TestCase* current_test_case() const; + const internal::TestCase* current_test_case() const; // Returns the TestInfo object for the test that's currently running, // or NULL if no test is running. -- cgit v1.2.3 From e6095deec89dcf5237948b3460d84a137622f16c Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 24 Jun 2009 23:02:50 +0000 Subject: Makes gtest's tuple implementation work with Symbian 5th edition by bypassing 2 compiler bugs (by Zhanyong Wan); refactors for the event listener API (by Vlad Losev). --- include/gtest/gtest.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 66653d1c..2b1c9782 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -418,6 +418,9 @@ class TestResult { // of successful test parts and the number of failed test parts. int total_part_count() const; + // Returns the number of the test properties. + int test_property_count() const; + // Returns true iff the test passed (i.e. no test part failed). bool Passed() const { return !Failed(); } @@ -436,6 +439,15 @@ class TestResult { // Sets the elapsed time. void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } + // Returns the i-th test part result among all the results. i can range + // from 0 to test_property_count() - 1. If i is not in that range, returns + // NULL. + const TestPartResult* GetTestPartResult(int i) const; + + // Returns the i-th test property. i can range from 0 to + // test_property_count() - 1. If i is not in that range, returns NULL. + const TestProperty* GetTestProperty(int i) const; + // Adds a test part result to the list. void AddTestPartResult(const TestPartResult& test_part_result); @@ -639,6 +651,10 @@ class TestCase { // Returns the elapsed time, in milliseconds. internal::TimeInMillis elapsed_time() const { return elapsed_time_; } + // Returns the i-th test among all the tests. i can range from 0 to + // total_test_count() - 1. If i is not in that range, returns NULL. + const TestInfo* GetTestInfo(int i) const; + // Adds a TestInfo to this test case. Will delete the TestInfo upon // destruction of the TestCase object. void AddTestInfo(TestInfo * test_info); @@ -799,7 +815,50 @@ class UnitTest { // Accessors for the implementation object. internal::UnitTestImpl* impl() { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; } + private: + // Gets the number of successful test cases. + int successful_test_case_count() const; + + // Gets the number of failed test cases. + int failed_test_case_count() const; + + // Gets the number of all test cases. + int total_test_case_count() const; + + // Gets the number of all test cases that contain at least one test + // that should run. + int test_case_to_run_count() const; + + // Gets the number of successful tests. + int successful_test_count() const; + + // Gets the number of failed tests. + int failed_test_count() const; + + // Gets the number of disabled tests. + int disabled_test_count() const; + + // Gets the number of all tests. + int total_test_count() const; + + // Gets the number of tests that should run. + int test_to_run_count() const; + + // Gets the elapsed time, in milliseconds. + internal::TimeInMillis elapsed_time() const; + + // Returns true iff the unit test passed (i.e. all test cases passed). + bool Passed() const; + + // Returns true iff the unit test failed (i.e. some test case failed + // or something outside of all tests failed). + bool Failed() const; + + // Gets the i-th test case among all the test cases. i can range from 0 to + // total_test_case_count() - 1. If i is not in that range, returns NULL. + const internal::TestCase* GetTestCase(int i) const; + // ScopedTrace is a friend as it needs to modify the per-thread // trace stack, which is a private member of UnitTest. friend class internal::ScopedTrace; -- cgit v1.2.3 From aaebfcdc4005afb22b68df61b58edd1ccc002913 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 25 Jun 2009 20:49:23 +0000 Subject: Refactors for the event listener API (by Vlad Losev): hides some methods in UnitTest; implements the result printers using the public API. --- include/gtest/gtest.h | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 28 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 2b1c9782..b2214705 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -141,8 +141,12 @@ const int kMaxStackTraceDepth = 100; namespace internal { +class AssertHelper; class GTestFlagSaver; class TestCase; // A collection of related tests. +class UnitTestImpl* GetUnitTestImpl(); +void ReportFailureInUnknownLocation(TestPartResultType result_type, + const String& message); // Converts a streamable value to a String. A NULL pointer is // converted to "(null)". When the input value is a ::string, @@ -759,33 +763,6 @@ class UnitTest { // Consecutive calls will return the same object. static UnitTest* GetInstance(); - // Registers and returns a global test environment. When a test - // program is run, all global test environments will be set-up in - // the order they were registered. After all tests in the program - // have finished, all global test environments will be torn-down in - // the *reverse* order they were registered. - // - // The UnitTest object takes ownership of the given environment. - // - // This method can only be called from the main thread. - Environment* AddEnvironment(Environment* env); - - // Adds a TestPartResult to the current TestResult object. All - // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) - // eventually call this to report their results. The user code - // should use the assertion macros instead of calling this directly. - // - // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - void AddTestPartResult(TestPartResultType result_type, - const char* file_name, - int line_number, - const internal::String& message, - const internal::String& os_stack_trace); - - // Adds a TestProperty to the current TestResult object. If the result already - // contains a property with the same key, the value will be updated. - void RecordPropertyForCurrentTest(const char* key, const char* value); - // Runs all tests in this UnitTest object and prints the result. // Returns 0 if successful, or 1 otherwise. // @@ -809,14 +786,41 @@ class UnitTest { #if GTEST_HAS_PARAM_TEST // Returns the ParameterizedTestCaseRegistry object used to keep track of // value-parameterized tests and instantiate and register them. + // + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. internal::ParameterizedTestCaseRegistry& parameterized_test_registry(); #endif // GTEST_HAS_PARAM_TEST + private: + // Registers and returns a global test environment. When a test + // program is run, all global test environments will be set-up in + // the order they were registered. After all tests in the program + // have finished, all global test environments will be torn-down in + // the *reverse* order they were registered. + // + // The UnitTest object takes ownership of the given environment. + // + // This method can only be called from the main thread. + Environment* AddEnvironment(Environment* env); + + // Adds a TestPartResult to the current TestResult object. All + // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) + // eventually call this to report their results. The user code + // should use the assertion macros instead of calling this directly. + void AddTestPartResult(TestPartResultType result_type, + const char* file_name, + int line_number, + const internal::String& message, + const internal::String& os_stack_trace); + + // Adds a TestProperty to the current TestResult object. If the result already + // contains a property with the same key, the value will be updated. + void RecordPropertyForCurrentTest(const char* key, const char* value); + // Accessors for the implementation object. internal::UnitTestImpl* impl() { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; } - private: // Gets the number of successful test cases. int successful_test_case_count() const; @@ -861,7 +865,20 @@ class UnitTest { // ScopedTrace is a friend as it needs to modify the per-thread // trace stack, which is a private member of UnitTest. + // TODO(vladl@google.com): Order all declarations according to the style + // guide after publishing of the above methods is done. friend class internal::ScopedTrace; + friend Environment* AddGlobalTestEnvironment(Environment* env); + friend internal::UnitTestImpl* internal::GetUnitTestImpl(); + friend class internal::AssertHelper; + friend class Test; + friend void internal::ReportFailureInUnknownLocation( + TestPartResultType result_type, + const internal::String& message); + // TODO(vladl@google.com): Remove these when publishing the new accessors. + friend class PrettyUnitTestResultPrinter; + friend class XmlUnitTestResultPrinter; + // Creates an empty UnitTest. UnitTest(); -- cgit v1.2.3 From b2db677c9905a34ca6454aa526f7a0cc5cfaeca1 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 1 Jul 2009 04:58:05 +0000 Subject: Reduces the flakiness of gtest-port_test on Mac; improves the Python tests; hides methods that we don't want to publish; makes win-dbg8 the default scons configuration (all by Vlad Losev). --- include/gtest/gtest.h | 83 +++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 35 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index b2214705..7d060787 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -142,8 +142,11 @@ const int kMaxStackTraceDepth = 100; namespace internal { class AssertHelper; +class DefaultGlobalTestPartResultReporter; +class ExecDeathTest; class GTestFlagSaver; class TestCase; // A collection of related tests. +class TestInfoImpl; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResultType result_type, const String& message); @@ -402,16 +405,6 @@ class TestResult { // D'tor. Do not inherit from TestResult. ~TestResult(); - // Gets the list of TestPartResults. - const internal::List& test_part_results() const { - return *test_part_results_; - } - - // Gets the list of TestProperties. - const internal::List& test_properties() const { - return *test_properties_; - } - // Gets the number of successful test parts. int successful_part_count() const; @@ -440,9 +433,6 @@ class TestResult { // Returns the elapsed time, in milliseconds. TimeInMillis elapsed_time() const { return elapsed_time_; } - // Sets the elapsed time. - void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } - // Returns the i-th test part result among all the results. i can range // from 0 to test_property_count() - 1. If i is not in that range, returns // NULL. @@ -452,8 +442,28 @@ class TestResult { // test_property_count() - 1. If i is not in that range, returns NULL. const TestProperty* GetTestProperty(int i) const; - // Adds a test part result to the list. - void AddTestPartResult(const TestPartResult& test_part_result); + private: + friend class DefaultGlobalTestPartResultReporter; + friend class ExecDeathTest; + friend class TestInfoImpl; + friend class TestResultAccessor; + friend class UnitTestImpl; + friend class WindowsDeathTest; + friend class testing::TestInfo; + friend class testing::UnitTest; + + // Gets the list of TestPartResults. + const internal::List& test_part_results() const { + return *test_part_results_; + } + + // Gets the list of TestProperties. + const internal::List& test_properties() const { + return *test_properties_; + } + + // Sets the elapsed time. + void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } // Adds a test property to the list. The property is validated and may add // a non-fatal failure if invalid (e.g., if it conflicts with reserved @@ -467,6 +477,9 @@ class TestResult { // TODO(russr): Validate attribute names are legal and human readable. static bool ValidateTestProperty(const internal::TestProperty& test_property); + // Adds a test part result to the list. + void AddTestPartResult(const TestPartResult& test_part_result); + // Returns the death test count. int death_test_count() const { return death_test_count_; } @@ -478,7 +491,7 @@ class TestResult { // Clears the object. void Clear(); - private: + // Protects mutable state of the property list and of owned properties, whose // values may be updated. internal::Mutex test_properites_mutex_; @@ -527,9 +540,6 @@ class TestInfo { // Returns the test comment. const char* comment() const; - // Returns true if this test matches the user-specified filter. - bool matches_filter() const; - // Returns true if this test should run, that is if the test is not disabled // (or it is disabled but the also_run_disabled_tests flag has been specified) // and its full name matches the user-specified filter. @@ -550,6 +560,7 @@ class TestInfo { // Returns the result of the test. const internal::TestResult* result() const; + private: #if GTEST_HAS_DEATH_TEST friend class internal::DefaultDeathTestFactory; @@ -566,6 +577,9 @@ class TestInfo { Test::TearDownTestCaseFunc tear_down_tc, internal::TestFactoryBase* factory); + // Returns true if this test matches the user-specified filter. + bool matches_filter() const; + // Increments the number of death tests encountered in this test so // far. int increment_death_test_count(); @@ -620,17 +634,6 @@ class TestCase { // Returns true if any test in this test case should run. bool should_run() const { return should_run_; } - // Sets the should_run member. - void set_should_run(bool should) { should_run_ = should; } - - // Gets the (mutable) list of TestInfos in this TestCase. - internal::List& test_info_list() { return *test_info_list_; } - - // Gets the (immutable) list of TestInfos in this TestCase. - const internal::List & test_info_list() const { - return *test_info_list_; - } - // Gets the number of successful tests in this test case. int successful_test_count() const; @@ -659,14 +662,25 @@ class TestCase { // total_test_count() - 1. If i is not in that range, returns NULL. const TestInfo* GetTestInfo(int i) const; + private: + friend class testing::Test; + friend class UnitTestImpl; + + // Gets the (mutable) list of TestInfos in this TestCase. + internal::List& test_info_list() { return *test_info_list_; } + + // Gets the (immutable) list of TestInfos in this TestCase. + const internal::List & test_info_list() const { + return *test_info_list_; + } + + // Sets the should_run member. + void set_should_run(bool should) { should_run_ = should; } + // Adds a TestInfo to this test case. Will delete the TestInfo upon // destruction of the TestCase object. void AddTestInfo(TestInfo * test_info); - // Finds and returns a TestInfo with the given name. If one doesn't - // exist, returns NULL. - TestInfo* GetTestInfo(const char* test_name); - // Clears the results of all tests in this test case. void ClearResult(); @@ -693,7 +707,6 @@ class TestCase { // Returns true if the given test should run. static bool ShouldRunTest(const TestInfo *test_info); - private: // Name of the test case. internal::String name_; // Comment on the test case. -- cgit v1.2.3 From 600105ee3ac48a01143486e5536a5b8fff5b5b25 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 1 Jul 2009 22:55:05 +0000 Subject: Makes List a random-access data structure. This simplifies the implementation and makes it easier to implement test shuffling. --- include/gtest/gtest.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 7d060787..f8aa91e8 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -434,13 +434,14 @@ class TestResult { TimeInMillis elapsed_time() const { return elapsed_time_; } // Returns the i-th test part result among all the results. i can range - // from 0 to test_property_count() - 1. If i is not in that range, returns - // NULL. - const TestPartResult* GetTestPartResult(int i) const; + // from 0 to test_property_count() - 1. If i is not in that range, aborts + // the program. + const TestPartResult& GetTestPartResult(int i) const; // Returns the i-th test property. i can range from 0 to - // test_property_count() - 1. If i is not in that range, returns NULL. - const TestProperty* GetTestProperty(int i) const; + // test_property_count() - 1. If i is not in that range, aborts the + // program. + const TestProperty& GetTestProperty(int i) const; private: friend class DefaultGlobalTestPartResultReporter; -- cgit v1.2.3 From 89080477aee9bd91536c9fb47bc31c62ea7d75bb Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 13 Jul 2009 19:25:02 +0000 Subject: Adds color support for TERM=linux (by Alexander Demin); renames List to Vector (by Zhanyong Wan); implements Vector::Erase (by Vlad Losev). --- include/gtest/gtest.h | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index f8aa91e8..5db7c18b 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -453,13 +453,13 @@ class TestResult { friend class testing::TestInfo; friend class testing::UnitTest; - // Gets the list of TestPartResults. - const internal::List& test_part_results() const { + // Gets the vector of TestPartResults. + const internal::Vector& test_part_results() const { return *test_part_results_; } - // Gets the list of TestProperties. - const internal::List& test_properties() const { + // Gets the vector of TestProperties. + const internal::Vector& test_properties() const { return *test_properties_; } @@ -493,14 +493,15 @@ class TestResult { // Clears the object. void Clear(); - // Protects mutable state of the property list and of owned properties, whose - // values may be updated. + // Protects mutable state of the property vector and of owned + // properties, whose values may be updated. internal::Mutex test_properites_mutex_; - // The list of TestPartResults - scoped_ptr > test_part_results_; - // The list of TestProperties - scoped_ptr > test_properties_; + // The vector of TestPartResults + internal::scoped_ptr > test_part_results_; + // The vector of TestProperties + internal::scoped_ptr > + test_properties_; // Running count of death tests. int death_test_count_; // The elapsed time, in milliseconds. @@ -604,7 +605,7 @@ class TestInfo { namespace internal { -// A test case, which consists of a list of TestInfos. +// A test case, which consists of a vector of TestInfos. // // TestCase is not copyable. class TestCase { @@ -667,11 +668,11 @@ class TestCase { friend class testing::Test; friend class UnitTestImpl; - // Gets the (mutable) list of TestInfos in this TestCase. - internal::List& test_info_list() { return *test_info_list_; } + // Gets the (mutable) vector of TestInfos in this TestCase. + internal::Vector& test_info_list() { return *test_info_list_; } - // Gets the (immutable) list of TestInfos in this TestCase. - const internal::List & test_info_list() const { + // Gets the (immutable) vector of TestInfos in this TestCase. + const internal::Vector & test_info_list() const { return *test_info_list_; } @@ -712,8 +713,8 @@ class TestCase { internal::String name_; // Comment on the test case. internal::String comment_; - // List of TestInfos. - internal::List* test_info_list_; + // Vector of TestInfos. + internal::Vector* test_info_list_; // Pointer to the function that sets up the test case. Test::SetUpTestCaseFunc set_up_tc_; // Pointer to the function that tears down the test case. @@ -760,7 +761,7 @@ class Environment { virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } }; -// A UnitTest consists of a list of TestCases. +// A UnitTest consists of a vector of TestCases. // // This is a singleton class. The only instance of UnitTest is // created when UnitTest::GetInstance() is first called. This -- cgit v1.2.3 From 8bdb31e0540c46de485b362178f60e8bb947ff43 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 14 Jul 2009 22:56:46 +0000 Subject: Adds the command line flags needed for test shuffling. Most code by Josh Kelley. --- include/gtest/gtest.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 5db7c18b..d6673d1a 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -119,6 +119,9 @@ GTEST_DECLARE_string_(output); // test. GTEST_DECLARE_bool_(print_time); +// This flag specifies the random number seed. +GTEST_DECLARE_int32_(random_seed); + // This flag sets how many times the tests are repeated. The default value // is 1. If the value is -1 the tests are repeating forever. GTEST_DECLARE_int32_(repeat); @@ -127,6 +130,9 @@ GTEST_DECLARE_int32_(repeat); // stack frames in failure stack traces. GTEST_DECLARE_bool_(show_internal_stack_frames); +// When this flag is specified, tests' order is randomized on every run. +GTEST_DECLARE_bool_(shuffle); + // This flag specifies the maximum number of stack frames to be // printed in a failure message. GTEST_DECLARE_int32_(stack_trace_depth); @@ -798,6 +804,9 @@ class UnitTest { // or NULL if no test is running. const TestInfo* current_test_info() const; + // Returns the random seed used at the start of the current test run. + int random_seed() const; + #if GTEST_HAS_PARAM_TEST // Returns the ParameterizedTestCaseRegistry object used to keep track of // value-parameterized tests and instantiate and register them. -- cgit v1.2.3 From c214ebc830aa918d54e535c6caa2da6317877e12 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 16 Jul 2009 00:36:55 +0000 Subject: More refactoring for the event listener API, by Vlad Losev. --- include/gtest/gtest.h | 54 +++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index d6673d1a..90c36a53 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -150,9 +150,13 @@ namespace internal { class AssertHelper; class DefaultGlobalTestPartResultReporter; class ExecDeathTest; +class FinalSuccessChecker; class GTestFlagSaver; -class TestCase; // A collection of related tests. +class TestCase; class TestInfoImpl; +class TestResultAccessor; +class UnitTestAccessor; +class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResultType result_type, const String& message); @@ -360,6 +364,8 @@ class Test { GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); }; +typedef internal::TimeInMillis TimeInMillis; + namespace internal { // A copyable object representing a user specified test property which can be @@ -392,9 +398,9 @@ class TestProperty { private: // The key supplied by the user. - String key_; + internal::String key_; // The value supplied by the user. - String value_; + internal::String value_; }; // The result of a single Test. This includes a list of @@ -411,12 +417,6 @@ class TestResult { // D'tor. Do not inherit from TestResult. ~TestResult(); - // Gets the number of successful test parts. - int successful_part_count() const; - - // Gets the number of failed test parts. - int failed_part_count() const; - // Gets the number of all test parts. This is the sum of the number // of successful test parts and the number of failed test parts. int total_part_count() const; @@ -428,7 +428,7 @@ class TestResult { bool Passed() const { return !Failed(); } // Returns true iff the test failed. - bool Failed() const { return failed_part_count() > 0; } + bool Failed() const; // Returns true iff the test fatally failed. bool HasFatalFailure() const; @@ -450,12 +450,12 @@ class TestResult { const TestProperty& GetTestProperty(int i) const; private: - friend class DefaultGlobalTestPartResultReporter; - friend class ExecDeathTest; - friend class TestInfoImpl; - friend class TestResultAccessor; - friend class UnitTestImpl; - friend class WindowsDeathTest; + friend class internal::DefaultGlobalTestPartResultReporter; + friend class internal::ExecDeathTest; + friend class internal::TestInfoImpl; + friend class internal::TestResultAccessor; + friend class internal::UnitTestImpl; + friend class internal::WindowsDeathTest; friend class testing::TestInfo; friend class testing::UnitTest; @@ -465,7 +465,7 @@ class TestResult { } // Gets the vector of TestProperties. - const internal::Vector& test_properties() const { + const internal::Vector& test_properties() const { return *test_properties_; } @@ -477,12 +477,12 @@ class TestResult { // key names). If a property is already recorded for the same key, the // value will be updated, rather than storing multiple values for the same // key. - void RecordProperty(const internal::TestProperty& test_property); + void RecordProperty(const TestProperty& test_property); // Adds a failure if the key is a reserved attribute of Google Test // testcase tags. Returns true if the property is valid. // TODO(russr): Validate attribute names are legal and human readable. - static bool ValidateTestProperty(const internal::TestProperty& test_property); + static bool ValidateTestProperty(const TestProperty& test_property); // Adds a test part result to the list. void AddTestPartResult(const TestPartResult& test_part_result); @@ -506,8 +506,7 @@ class TestResult { // The vector of TestPartResults internal::scoped_ptr > test_part_results_; // The vector of TestProperties - internal::scoped_ptr > - test_properties_; + internal::scoped_ptr > test_properties_; // Running count of death tests. int death_test_count_; // The elapsed time, in milliseconds. @@ -664,7 +663,7 @@ class TestCase { bool Failed() const { return failed_test_count() > 0; } // Returns the elapsed time, in milliseconds. - internal::TimeInMillis elapsed_time() const { return elapsed_time_; } + TimeInMillis elapsed_time() const { return elapsed_time_; } // Returns the i-th test among all the tests. i can range from 0 to // total_test_count() - 1. If i is not in that range, returns NULL. @@ -672,7 +671,7 @@ class TestCase { private: friend class testing::Test; - friend class UnitTestImpl; + friend class internal::UnitTestImpl; // Gets the (mutable) vector of TestInfos in this TestCase. internal::Vector& test_info_list() { return *test_info_list_; } @@ -728,7 +727,7 @@ class TestCase { // True iff any test in this test case should run. bool should_run_; // Elapsed time, in milliseconds. - internal::TimeInMillis elapsed_time_; + TimeInMillis elapsed_time_; // We disallow copying TestCases. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); @@ -874,7 +873,7 @@ class UnitTest { int test_to_run_count() const; // Gets the elapsed time, in milliseconds. - internal::TimeInMillis elapsed_time() const; + TimeInMillis elapsed_time() const; // Returns true iff the unit test passed (i.e. all test cases passed). bool Passed() const; @@ -902,6 +901,11 @@ class UnitTest { // TODO(vladl@google.com): Remove these when publishing the new accessors. friend class PrettyUnitTestResultPrinter; friend class XmlUnitTestResultPrinter; + friend class internal::UnitTestAccessor; + friend class FinalSuccessChecker; + FRIEND_TEST(ApiTest, UnitTestImmutableAccessorsWork); + FRIEND_TEST(ApiTest, TestCaseImmutableAccessorsWork); + FRIEND_TEST(ApiTest, DisabledTestCaseAccessorsWork); // Creates an empty UnitTest. -- cgit v1.2.3 From 16e9dd6e28a8a7fb2d611011e7353e042fcb282f Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 4 Sep 2009 18:30:25 +0000 Subject: More implementation of the event listener interface (by Vlad Losev); Reduces the stack space usage of assertions by moving AssertHelper's fields to the heap (by Jorg Brown); Makes String faster, smaller, and simpler (by Zhanyong Wan); Fixes a bug in String::Format() (by Chandler); Adds the /MD version of VC projects to the distribution (by Vlad Losev). --- include/gtest/gtest.h | 215 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 209 insertions(+), 6 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 90c36a53..0727adbd 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -149,17 +149,23 @@ namespace internal { class AssertHelper; class DefaultGlobalTestPartResultReporter; +class EventListenersAccessor; class ExecDeathTest; +class NoExecDeathTest; class FinalSuccessChecker; class GTestFlagSaver; class TestCase; class TestInfoImpl; class TestResultAccessor; class UnitTestAccessor; +// TODO(vladl@google.com): Rename to TestEventRepeater. +class UnitTestEventsRepeater; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResultType result_type, const String& message); +class PrettyUnitTestResultPrinter; +class XmlUnitTestResultPrinter; // Converts a streamable value to a String. A NULL pointer is // converted to "(null)". When the input value is a ::string, @@ -766,6 +772,178 @@ class Environment { virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } }; +namespace internal { + +// TODO(vladl@google.com): Order the methods the way they are invoked by +// Google Test. +// The interface for tracing execution of tests. +class UnitTestEventListenerInterface { + public: + virtual ~UnitTestEventListenerInterface() {} + + // TODO(vladl@google.com): Add events for test program start and test program + // end: OnTestIterationStart(const UnitTest&); // Start of one iteration. + // Add tests, too. + // TODO(vladl@google.com): Rename OnUnitTestStart() and OnUnitTestEnd() to + // OnTestProgramStart() and OnTestProgramEnd(). + // Called before any test activity starts. + virtual void OnUnitTestStart(const UnitTest& unit_test) = 0; + + // Called after all test activities have ended. + virtual void OnUnitTestEnd(const UnitTest& unit_test) = 0; + + // Called before the test case starts. + virtual void OnTestCaseStart(const TestCase& test_case) = 0; + + // Called after the test case ends. + virtual void OnTestCaseEnd(const TestCase& test_case) = 0; + + // TODO(vladl@google.com): Rename OnGlobalSetUpStart to + // OnEnvironmentsSetUpStart. Make similar changes for the rest of + // environment-related events. + // Called before the global set-up starts. + virtual void OnGlobalSetUpStart(const UnitTest& unit_test) = 0; + + // Called after the global set-up ends. + virtual void OnGlobalSetUpEnd(const UnitTest& unit_test) = 0; + + // Called before the global tear-down starts. + virtual void OnGlobalTearDownStart(const UnitTest& unit_test) = 0; + + // Called after the global tear-down ends. + virtual void OnGlobalTearDownEnd(const UnitTest& unit_test) = 0; + + // Called before the test starts. + virtual void OnTestStart(const TestInfo& test_info) = 0; + + // Called after the test ends. + virtual void OnTestEnd(const TestInfo& test_info) = 0; + + // Called after a failed assertion or a SUCCESS(). + virtual void OnNewTestPartResult(const TestPartResult& test_part_result) = 0; +}; + +// The convenience class for users who need to override just one or two +// methods and are not concerned that a possible change to a signature of +// the methods they override will not be caught during the build. +class EmptyTestEventListener : public UnitTestEventListenerInterface { + public: + // Called before the unit test starts. + virtual void OnUnitTestStart(const UnitTest& /*unit_test*/) {} + + // Called after the unit test ends. + virtual void OnUnitTestEnd(const UnitTest& /*unit_test*/) {} + + // Called before the test case starts. + virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} + + // Called after the test case ends. + virtual void OnTestCaseEnd(const TestCase& /*test_case&*/) {} + + // Called before the global set-up starts. + virtual void OnGlobalSetUpStart(const UnitTest& /*unit_test*/) {} + + // Called after the global set-up ends. + virtual void OnGlobalSetUpEnd(const UnitTest& /*unit_test*/) {} + + // Called before the global tear-down starts. + virtual void OnGlobalTearDownStart(const UnitTest& /*unit_test*/) {} + + // Called after the global tear-down ends. + virtual void OnGlobalTearDownEnd(const UnitTest& /*unit_test*/) {} + + // Called before the test starts. + virtual void OnTestStart(const TestInfo& /*test_info*/) {} + + // Called after the test ends. + virtual void OnTestEnd(const TestInfo& /*test_info*/) {} + + // Called after a failed assertion or a SUCCESS(). + virtual void OnNewTestPartResult(const TestPartResult& /*test_part_result*/) { + } +}; + +// EventListeners lets users add listeners to track events in Google Test. +class EventListeners { + public: + EventListeners(); + ~EventListeners(); + + // Appends an event listener to the end of the list. Google Test assumes + // the ownership of the listener (i.e. it will delete the listener when + // the test program finishes). + void Append(UnitTestEventListenerInterface* listener); + + // Removes the given event listener from the list and returns it. It then + // becomes the caller's responsibility to delete the listener. Returns + // NULL if the listener is not found in the list. + UnitTestEventListenerInterface* Release( + UnitTestEventListenerInterface* listener); + + // Returns the standard listener responsible for the default console + // output. Can be removed from the listeners list to shut down default + // console output. Note that removing this object from the listener list + // with Release transfers its ownership to the caller and makes this + // function return NULL the next time. + UnitTestEventListenerInterface* default_result_printer() const { + return default_result_printer_; + } + + // Returns the standard listener responsible for the default XML output + // controlled by the --gtest_output=xml flag. Can be removed from the + // listeners list by users who want to shut down the default XML output + // controlled by this flag and substitute it with custom one. Note that + // removing this object from the listener list with Release transfers its + // ownership to the caller and makes this function return NULL the next + // time. + UnitTestEventListenerInterface* default_xml_generator() const { + return default_xml_generator_; + } + + private: + friend class internal::DefaultGlobalTestPartResultReporter; + friend class internal::EventListenersAccessor; + friend class internal::NoExecDeathTest; + friend class internal::TestCase; + friend class internal::TestInfoImpl; + friend class internal::UnitTestImpl; + + // Returns repeater that broadcasts the UnitTestEventListenerInterface + // events to all subscribers. + UnitTestEventListenerInterface* repeater(); + + // Sets the default_result_printer attribute to the provided listener. + // The listener is also added to the listener list and previous + // default_result_printer is removed from it and deleted. The listener can + // also be NULL in which case it will not be added to the list. Does + // nothing if the previous and the current listener objects are the same. + void SetDefaultResultPrinter(UnitTestEventListenerInterface* listener); + + // Sets the default_xml_generator attribute to the provided listener. The + // listener is also added to the listener list and previous + // default_xml_generator is removed from it and deleted. The listener can + // also be NULL in which case it will not be added to the list. Does + // nothing if the previous and the current listener objects are the same. + void SetDefaultXmlGenerator(UnitTestEventListenerInterface* listener); + + // Controls whether events will be forwarded by the repeater to the + // listeners in the list. + bool EventForwardingEnabled() const; + void SuppressEventForwarding(); + + // The actual list of listeners. + internal::UnitTestEventsRepeater* repeater_; + // Listener responsible for the standard result output. + UnitTestEventListenerInterface* default_result_printer_; + // Listener responsible for the creation of the XML output file. + UnitTestEventListenerInterface* default_xml_generator_; + + // We disallow copying EventListeners. + GTEST_DISALLOW_COPY_AND_ASSIGN_(EventListeners); +}; + +} // namespace internal + // A UnitTest consists of a vector of TestCases. // // This is a singleton class. The only instance of UnitTest is @@ -886,6 +1064,10 @@ class UnitTest { // total_test_case_count() - 1. If i is not in that range, returns NULL. const internal::TestCase* GetTestCase(int i) const; + // Returns the list of event listeners that can be used to track events + // inside Google Test. + internal::EventListeners& listeners(); + // ScopedTrace is a friend as it needs to modify the per-thread // trace stack, which is a private member of UnitTest. // TODO(vladl@google.com): Order all declarations according to the style @@ -899,9 +1081,12 @@ class UnitTest { TestPartResultType result_type, const internal::String& message); // TODO(vladl@google.com): Remove these when publishing the new accessors. - friend class PrettyUnitTestResultPrinter; - friend class XmlUnitTestResultPrinter; + friend class internal::PrettyUnitTestResultPrinter; + friend class internal::TestCase; + friend class internal::TestInfoImpl; friend class internal::UnitTestAccessor; + friend class internal::UnitTestImpl; + friend class internal::XmlUnitTestResultPrinter; friend class FinalSuccessChecker; FRIEND_TEST(ApiTest, UnitTestImmutableAccessorsWork); FRIEND_TEST(ApiTest, TestCaseImmutableAccessorsWork); @@ -1299,14 +1484,32 @@ class AssertHelper { // Constructor. AssertHelper(TestPartResultType type, const char* file, int line, const char* message); + ~AssertHelper(); + // Message assignment is a semantic trick to enable assertion // streaming; see the GTEST_MESSAGE_ macro below. void operator=(const Message& message) const; + private: - TestPartResultType const type_; - const char* const file_; - int const line_; - String const message_; + // We put our data in a struct so that the size of the AssertHelper class can + // be as small as possible. This is important because gcc is incapable of + // re-using stack space even for temporary variables, so every EXPECT_EQ + // reserves stack space for another AssertHelper. + struct AssertHelperData { + AssertHelperData(TestPartResultType t, const char* srcfile, int line_num, + const char* msg) + : type(t), file(srcfile), line(line_num), message(msg) { } + + TestPartResultType const type; + const char* const file; + int const line; + String const message; + + private: + GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); + }; + + AssertHelperData* const data_; GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); }; -- cgit v1.2.3 From f43e4ff3ad12ace9d423cc3ce02feadb8f24fe67 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 17 Sep 2009 19:12:30 +0000 Subject: Renames the methods in the event listener API, and changes the order of *End events (by Vlad Losev). --- include/gtest/gtest.h | 110 ++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 61 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 0727adbd..ecbbf9be 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -158,8 +158,7 @@ class TestCase; class TestInfoImpl; class TestResultAccessor; class UnitTestAccessor; -// TODO(vladl@google.com): Rename to TestEventRepeater. -class UnitTestEventsRepeater; +class TestEventRepeater; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResultType result_type, @@ -781,86 +780,75 @@ class UnitTestEventListenerInterface { public: virtual ~UnitTestEventListenerInterface() {} - // TODO(vladl@google.com): Add events for test program start and test program - // end: OnTestIterationStart(const UnitTest&); // Start of one iteration. - // Add tests, too. - // TODO(vladl@google.com): Rename OnUnitTestStart() and OnUnitTestEnd() to - // OnTestProgramStart() and OnTestProgramEnd(). - // Called before any test activity starts. - virtual void OnUnitTestStart(const UnitTest& unit_test) = 0; + // TODO(vladl@google.com): Add tests for OnTestIterationStart and + // OnTestIterationEnd. - // Called after all test activities have ended. - virtual void OnUnitTestEnd(const UnitTest& unit_test) = 0; + // Fired before any test activity starts. + virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; - // Called before the test case starts. - virtual void OnTestCaseStart(const TestCase& test_case) = 0; + // Fired after all test activities have ended. + virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; - // Called after the test case ends. - virtual void OnTestCaseEnd(const TestCase& test_case) = 0; + // Fired before each iteration of tests starts. There may be more than + // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration + // index, starting from 0. + virtual void OnTestIterationStart(const UnitTest& unit_test, + int iteration) = 0; + + // Fired after each iteration of tests finishes. + virtual void OnTestIterationEnd(const UnitTest& unit_test, + int iteration) = 0; - // TODO(vladl@google.com): Rename OnGlobalSetUpStart to - // OnEnvironmentsSetUpStart. Make similar changes for the rest of - // environment-related events. - // Called before the global set-up starts. - virtual void OnGlobalSetUpStart(const UnitTest& unit_test) = 0; + // Fired before environment set-up for each iteration of tests starts. + virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; - // Called after the global set-up ends. - virtual void OnGlobalSetUpEnd(const UnitTest& unit_test) = 0; + // Fired after environment set-up for each iteration of tests ends. + virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; - // Called before the global tear-down starts. - virtual void OnGlobalTearDownStart(const UnitTest& unit_test) = 0; + // Fired before environment tear-down for each iteration of tests starts. + virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; - // Called after the global tear-down ends. - virtual void OnGlobalTearDownEnd(const UnitTest& unit_test) = 0; + // Fired after environment tear-down for each iteration of tests ends. + virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; + + // Fired before the test case starts. + virtual void OnTestCaseStart(const TestCase& test_case) = 0; - // Called before the test starts. + // Fired after the test case ends. + virtual void OnTestCaseEnd(const TestCase& test_case) = 0; + + // Fired before the test starts. virtual void OnTestStart(const TestInfo& test_info) = 0; - // Called after the test ends. + // Fired after the test ends. virtual void OnTestEnd(const TestInfo& test_info) = 0; - // Called after a failed assertion or a SUCCESS(). - virtual void OnNewTestPartResult(const TestPartResult& test_part_result) = 0; + // Fired after a failed assertion or a SUCCESS(). + virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; }; // The convenience class for users who need to override just one or two // methods and are not concerned that a possible change to a signature of // the methods they override will not be caught during the build. +// For comments about each method please see the definition of +// UnitTestEventListenerInterface above. class EmptyTestEventListener : public UnitTestEventListenerInterface { public: - // Called before the unit test starts. - virtual void OnUnitTestStart(const UnitTest& /*unit_test*/) {} - - // Called after the unit test ends. - virtual void OnUnitTestEnd(const UnitTest& /*unit_test*/) {} - - // Called before the test case starts. + virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} + virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} + virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, + int /*iteration*/) {} + virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, + int /*iteration*/) {} + virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {} + virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {} + virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {} + virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} - - // Called after the test case ends. - virtual void OnTestCaseEnd(const TestCase& /*test_case&*/) {} - - // Called before the global set-up starts. - virtual void OnGlobalSetUpStart(const UnitTest& /*unit_test*/) {} - - // Called after the global set-up ends. - virtual void OnGlobalSetUpEnd(const UnitTest& /*unit_test*/) {} - - // Called before the global tear-down starts. - virtual void OnGlobalTearDownStart(const UnitTest& /*unit_test*/) {} - - // Called after the global tear-down ends. - virtual void OnGlobalTearDownEnd(const UnitTest& /*unit_test*/) {} - - // Called before the test starts. + virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} virtual void OnTestStart(const TestInfo& /*test_info*/) {} - - // Called after the test ends. virtual void OnTestEnd(const TestInfo& /*test_info*/) {} - - // Called after a failed assertion or a SUCCESS(). - virtual void OnNewTestPartResult(const TestPartResult& /*test_part_result*/) { - } + virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {} }; // EventListeners lets users add listeners to track events in Google Test. @@ -932,7 +920,7 @@ class EventListeners { void SuppressEventForwarding(); // The actual list of listeners. - internal::UnitTestEventsRepeater* repeater_; + internal::TestEventRepeater* repeater_; // Listener responsible for the standard result output. UnitTestEventListenerInterface* default_result_printer_; // Listener responsible for the creation of the XML output file. -- cgit v1.2.3 From e5373af0cb9cae249e7bc618cae3483397332895 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 18 Sep 2009 18:16:20 +0000 Subject: Renames the TestPartResult type enums and adjusts the order of methods in the event listener interface (by Vlad Losev). --- include/gtest/gtest.h | 74 +++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index ecbbf9be..5c928767 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -161,7 +161,7 @@ class UnitTestAccessor; class TestEventRepeater; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); -void ReportFailureInUnknownLocation(TestPartResultType result_type, +void ReportFailureInUnknownLocation(TestPartResult::Type result_type, const String& message); class PrettyUnitTestResultPrinter; class XmlUnitTestResultPrinter; @@ -773,58 +773,54 @@ class Environment { namespace internal { -// TODO(vladl@google.com): Order the methods the way they are invoked by -// Google Test. -// The interface for tracing execution of tests. +// The interface for tracing execution of tests. The methods are organized in +// the order the corresponding events are fired. class UnitTestEventListenerInterface { public: virtual ~UnitTestEventListenerInterface() {} - // TODO(vladl@google.com): Add tests for OnTestIterationStart and - // OnTestIterationEnd. - // Fired before any test activity starts. virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; - // Fired after all test activities have ended. - virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; - // Fired before each iteration of tests starts. There may be more than // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration // index, starting from 0. virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration) = 0; - // Fired after each iteration of tests finishes. - virtual void OnTestIterationEnd(const UnitTest& unit_test, - int iteration) = 0; - // Fired before environment set-up for each iteration of tests starts. virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; // Fired after environment set-up for each iteration of tests ends. virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; - // Fired before environment tear-down for each iteration of tests starts. - virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; - - // Fired after environment tear-down for each iteration of tests ends. - virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; - // Fired before the test case starts. virtual void OnTestCaseStart(const TestCase& test_case) = 0; - // Fired after the test case ends. - virtual void OnTestCaseEnd(const TestCase& test_case) = 0; - // Fired before the test starts. virtual void OnTestStart(const TestInfo& test_info) = 0; + // Fired after a failed assertion or a SUCCESS(). + virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; + // Fired after the test ends. virtual void OnTestEnd(const TestInfo& test_info) = 0; - // Fired after a failed assertion or a SUCCESS(). - virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; + // Fired after the test case ends. + virtual void OnTestCaseEnd(const TestCase& test_case) = 0; + + // Fired before environment tear-down for each iteration of tests starts. + virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; + + // Fired after environment tear-down for each iteration of tests ends. + virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; + + // Fired after each iteration of tests finishes. + virtual void OnTestIterationEnd(const UnitTest& unit_test, + int iteration) = 0; + + // Fired after all test activities have ended. + virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; }; // The convenience class for users who need to override just one or two @@ -835,20 +831,20 @@ class UnitTestEventListenerInterface { class EmptyTestEventListener : public UnitTestEventListenerInterface { public: virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} - virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, int /*iteration*/) {} - virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, - int /*iteration*/) {} virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {} virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {} - virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {} - virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} - virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} virtual void OnTestStart(const TestInfo& /*test_info*/) {} - virtual void OnTestEnd(const TestInfo& /*test_info*/) {} virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {} + virtual void OnTestEnd(const TestInfo& /*test_info*/) {} + virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} + virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {} + virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} + virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, + int /*iteration*/) {} + virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} }; // EventListeners lets users add listeners to track events in Google Test. @@ -996,7 +992,7 @@ class UnitTest { // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) // eventually call this to report their results. The user code // should use the assertion macros instead of calling this directly. - void AddTestPartResult(TestPartResultType result_type, + void AddTestPartResult(TestPartResult::Type result_type, const char* file_name, int line_number, const internal::String& message, @@ -1066,7 +1062,7 @@ class UnitTest { friend class internal::AssertHelper; friend class Test; friend void internal::ReportFailureInUnknownLocation( - TestPartResultType result_type, + TestPartResult::Type result_type, const internal::String& message); // TODO(vladl@google.com): Remove these when publishing the new accessors. friend class internal::PrettyUnitTestResultPrinter; @@ -1470,7 +1466,9 @@ AssertionResult DoubleNearPredFormat(const char* expr1, class AssertHelper { public: // Constructor. - AssertHelper(TestPartResultType type, const char* file, int line, + AssertHelper(TestPartResult::Type type, + const char* file, + int line, const char* message); ~AssertHelper(); @@ -1484,11 +1482,13 @@ class AssertHelper { // re-using stack space even for temporary variables, so every EXPECT_EQ // reserves stack space for another AssertHelper. struct AssertHelperData { - AssertHelperData(TestPartResultType t, const char* srcfile, int line_num, + AssertHelperData(TestPartResult::Type t, + const char* srcfile, + int line_num, const char* msg) : type(t), file(srcfile), line(line_num), message(msg) { } - TestPartResultType const type; + TestPartResult::Type const type; const char* const file; int const line; String const message; -- cgit v1.2.3 From b50ef44a3527d958270ff1f08cb99e3ac633bd17 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 24 Sep 2009 21:15:59 +0000 Subject: Publishes the even listener API (by Vlad Losev); adds OS-indicating macros to simplify gtest code (by Zhanyong Wan). --- include/gtest/gtest.h | 158 ++++++++++++++++++++------------------------------ 1 file changed, 63 insertions(+), 95 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 5c928767..2f15fa31 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -51,9 +51,6 @@ #ifndef GTEST_INCLUDE_GTEST_GTEST_H_ #define GTEST_INCLUDE_GTEST_GTEST_H_ -// The following platform macro is used throughout Google Test: -// _WIN32_WCE Windows CE (set in project files) - #include #include #include @@ -154,10 +151,8 @@ class ExecDeathTest; class NoExecDeathTest; class FinalSuccessChecker; class GTestFlagSaver; -class TestCase; class TestInfoImpl; class TestResultAccessor; -class UnitTestAccessor; class TestEventRepeater; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); @@ -371,8 +366,6 @@ class Test { typedef internal::TimeInMillis TimeInMillis; -namespace internal { - // A copyable object representing a user specified test property which can be // output as a key/value string pair. // @@ -455,14 +448,14 @@ class TestResult { const TestProperty& GetTestProperty(int i) const; private: + friend class TestInfo; + friend class UnitTest; friend class internal::DefaultGlobalTestPartResultReporter; friend class internal::ExecDeathTest; friend class internal::TestInfoImpl; friend class internal::TestResultAccessor; friend class internal::UnitTestImpl; friend class internal::WindowsDeathTest; - friend class testing::TestInfo; - friend class testing::UnitTest; // Gets the vector of TestPartResults. const internal::Vector& test_part_results() const { @@ -521,8 +514,6 @@ class TestResult { GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); }; // class TestResult -} // namespace internal - // A TestInfo object stores the following information about a test: // // Test case name @@ -571,16 +562,16 @@ class TestInfo { bool should_run() const; // Returns the result of the test. - const internal::TestResult* result() const; + const TestResult* result() const; private: #if GTEST_HAS_DEATH_TEST friend class internal::DefaultDeathTestFactory; #endif // GTEST_HAS_DEATH_TEST + friend class Test; + friend class TestCase; friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; - friend class Test; - friend class internal::TestCase; friend TestInfo* internal::MakeAndRegisterTestInfo( const char* test_case_name, const char* name, const char* test_case_comment, const char* comment, @@ -613,8 +604,6 @@ class TestInfo { GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); }; -namespace internal { - // A test case, which consists of a vector of TestInfos. // // TestCase is not copyable. @@ -675,7 +664,7 @@ class TestCase { const TestInfo* GetTestInfo(int i) const; private: - friend class testing::Test; + friend class Test; friend class internal::UnitTestImpl; // Gets the (mutable) vector of TestInfos in this TestCase. @@ -738,8 +727,6 @@ class TestCase { GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); }; -} // namespace internal - // An Environment object is capable of setting up and tearing down an // environment. The user should subclass this to define his own // environment(s). @@ -771,13 +758,11 @@ class Environment { virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } }; -namespace internal { - // The interface for tracing execution of tests. The methods are organized in // the order the corresponding events are fired. -class UnitTestEventListenerInterface { +class TestEventListener { public: - virtual ~UnitTestEventListenerInterface() {} + virtual ~TestEventListener() {} // Fired before any test activity starts. virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; @@ -825,10 +810,10 @@ class UnitTestEventListenerInterface { // The convenience class for users who need to override just one or two // methods and are not concerned that a possible change to a signature of -// the methods they override will not be caught during the build. -// For comments about each method please see the definition of -// UnitTestEventListenerInterface above. -class EmptyTestEventListener : public UnitTestEventListenerInterface { +// the methods they override will not be caught during the build. For +// comments about each method please see the definition of TestEventListener +// above. +class EmptyTestEventListener : public TestEventListener { public: virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, @@ -850,26 +835,25 @@ class EmptyTestEventListener : public UnitTestEventListenerInterface { // EventListeners lets users add listeners to track events in Google Test. class EventListeners { public: - EventListeners(); - ~EventListeners(); + EventListeners(); + ~EventListeners(); // Appends an event listener to the end of the list. Google Test assumes // the ownership of the listener (i.e. it will delete the listener when // the test program finishes). - void Append(UnitTestEventListenerInterface* listener); + void Append(TestEventListener* listener); // Removes the given event listener from the list and returns it. It then // becomes the caller's responsibility to delete the listener. Returns // NULL if the listener is not found in the list. - UnitTestEventListenerInterface* Release( - UnitTestEventListenerInterface* listener); + TestEventListener* Release(TestEventListener* listener); // Returns the standard listener responsible for the default console // output. Can be removed from the listeners list to shut down default // console output. Note that removing this object from the listener list // with Release transfers its ownership to the caller and makes this // function return NULL the next time. - UnitTestEventListenerInterface* default_result_printer() const { + TestEventListener* default_result_printer() const { return default_result_printer_; } @@ -880,35 +864,35 @@ class EventListeners { // removing this object from the listener list with Release transfers its // ownership to the caller and makes this function return NULL the next // time. - UnitTestEventListenerInterface* default_xml_generator() const { + TestEventListener* default_xml_generator() const { return default_xml_generator_; } private: + friend class TestCase; friend class internal::DefaultGlobalTestPartResultReporter; friend class internal::EventListenersAccessor; friend class internal::NoExecDeathTest; - friend class internal::TestCase; friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; - // Returns repeater that broadcasts the UnitTestEventListenerInterface - // events to all subscribers. - UnitTestEventListenerInterface* repeater(); + // Returns repeater that broadcasts the TestEventListener events to all + // subscribers. + TestEventListener* repeater(); // Sets the default_result_printer attribute to the provided listener. // The listener is also added to the listener list and previous // default_result_printer is removed from it and deleted. The listener can // also be NULL in which case it will not be added to the list. Does // nothing if the previous and the current listener objects are the same. - void SetDefaultResultPrinter(UnitTestEventListenerInterface* listener); + void SetDefaultResultPrinter(TestEventListener* listener); // Sets the default_xml_generator attribute to the provided listener. The // listener is also added to the listener list and previous // default_xml_generator is removed from it and deleted. The listener can // also be NULL in which case it will not be added to the list. Does // nothing if the previous and the current listener objects are the same. - void SetDefaultXmlGenerator(UnitTestEventListenerInterface* listener); + void SetDefaultXmlGenerator(TestEventListener* listener); // Controls whether events will be forwarded by the repeater to the // listeners in the list. @@ -918,16 +902,14 @@ class EventListeners { // The actual list of listeners. internal::TestEventRepeater* repeater_; // Listener responsible for the standard result output. - UnitTestEventListenerInterface* default_result_printer_; + TestEventListener* default_result_printer_; // Listener responsible for the creation of the XML output file. - UnitTestEventListenerInterface* default_xml_generator_; + TestEventListener* default_xml_generator_; // We disallow copying EventListeners. GTEST_DISALLOW_COPY_AND_ASSIGN_(EventListeners); }; -} // namespace internal - // A UnitTest consists of a vector of TestCases. // // This is a singleton class. The only instance of UnitTest is @@ -959,7 +941,7 @@ class UnitTest { // Returns the TestCase object for the test that's currently running, // or NULL if no test is running. - const internal::TestCase* current_test_case() const; + const TestCase* current_test_case() const; // Returns the TestInfo object for the test that's currently running, // or NULL if no test is running. @@ -976,36 +958,6 @@ class UnitTest { internal::ParameterizedTestCaseRegistry& parameterized_test_registry(); #endif // GTEST_HAS_PARAM_TEST - private: - // Registers and returns a global test environment. When a test - // program is run, all global test environments will be set-up in - // the order they were registered. After all tests in the program - // have finished, all global test environments will be torn-down in - // the *reverse* order they were registered. - // - // The UnitTest object takes ownership of the given environment. - // - // This method can only be called from the main thread. - Environment* AddEnvironment(Environment* env); - - // Adds a TestPartResult to the current TestResult object. All - // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) - // eventually call this to report their results. The user code - // should use the assertion macros instead of calling this directly. - void AddTestPartResult(TestPartResult::Type result_type, - const char* file_name, - int line_number, - const internal::String& message, - const internal::String& os_stack_trace); - - // Adds a TestProperty to the current TestResult object. If the result already - // contains a property with the same key, the value will be updated. - void RecordPropertyForCurrentTest(const char* key, const char* value); - - // Accessors for the implementation object. - internal::UnitTestImpl* impl() { return impl_; } - const internal::UnitTestImpl* impl() const { return impl_; } - // Gets the number of successful test cases. int successful_test_case_count() const; @@ -1046,36 +998,52 @@ class UnitTest { // Gets the i-th test case among all the test cases. i can range from 0 to // total_test_case_count() - 1. If i is not in that range, returns NULL. - const internal::TestCase* GetTestCase(int i) const; + const TestCase* GetTestCase(int i) const; // Returns the list of event listeners that can be used to track events // inside Google Test. - internal::EventListeners& listeners(); + EventListeners& listeners(); - // ScopedTrace is a friend as it needs to modify the per-thread - // trace stack, which is a private member of UnitTest. - // TODO(vladl@google.com): Order all declarations according to the style - // guide after publishing of the above methods is done. + private: + // Registers and returns a global test environment. When a test + // program is run, all global test environments will be set-up in + // the order they were registered. After all tests in the program + // have finished, all global test environments will be torn-down in + // the *reverse* order they were registered. + // + // The UnitTest object takes ownership of the given environment. + // + // This method can only be called from the main thread. + Environment* AddEnvironment(Environment* env); + + // Adds a TestPartResult to the current TestResult object. All + // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) + // eventually call this to report their results. The user code + // should use the assertion macros instead of calling this directly. + void AddTestPartResult(TestPartResult::Type result_type, + const char* file_name, + int line_number, + const internal::String& message, + const internal::String& os_stack_trace); + + // Adds a TestProperty to the current TestResult object. If the result already + // contains a property with the same key, the value will be updated. + void RecordPropertyForCurrentTest(const char* key, const char* value); + + // Accessors for the implementation object. + internal::UnitTestImpl* impl() { return impl_; } + const internal::UnitTestImpl* impl() const { return impl_; } + + // These classes and funcions are friends as they need to access private + // members of UnitTest. + friend class Test; + friend class internal::AssertHelper; friend class internal::ScopedTrace; friend Environment* AddGlobalTestEnvironment(Environment* env); friend internal::UnitTestImpl* internal::GetUnitTestImpl(); - friend class internal::AssertHelper; - friend class Test; friend void internal::ReportFailureInUnknownLocation( TestPartResult::Type result_type, const internal::String& message); - // TODO(vladl@google.com): Remove these when publishing the new accessors. - friend class internal::PrettyUnitTestResultPrinter; - friend class internal::TestCase; - friend class internal::TestInfoImpl; - friend class internal::UnitTestAccessor; - friend class internal::UnitTestImpl; - friend class internal::XmlUnitTestResultPrinter; - friend class FinalSuccessChecker; - FRIEND_TEST(ApiTest, UnitTestImmutableAccessorsWork); - FRIEND_TEST(ApiTest, TestCaseImmutableAccessorsWork); - FRIEND_TEST(ApiTest, DisabledTestCaseAccessorsWork); - // Creates an empty UnitTest. UnitTest(); -- cgit v1.2.3 From f8b268ee86ca74bba3276352f1e7de53d1336c3e Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 30 Sep 2009 20:23:50 +0000 Subject: Makes gtest compile cleanly with MSVC's /W4 (by Zhanyong Wan). Renames EventListenrs to TestEventListeners (by Zhanyong Wan). Fixes invalid characters in XML report (by Vlad Losev). Refacotrs SConscript (by Vlad Losev). --- include/gtest/gtest.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 2f15fa31..6fc5ac5c 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -146,13 +146,13 @@ namespace internal { class AssertHelper; class DefaultGlobalTestPartResultReporter; -class EventListenersAccessor; class ExecDeathTest; class NoExecDeathTest; class FinalSuccessChecker; class GTestFlagSaver; class TestInfoImpl; class TestResultAccessor; +class TestEventListenersAccessor; class TestEventRepeater; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); @@ -832,11 +832,11 @@ class EmptyTestEventListener : public TestEventListener { virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} }; -// EventListeners lets users add listeners to track events in Google Test. -class EventListeners { +// TestEventListeners lets users add listeners to track events in Google Test. +class TestEventListeners { public: - EventListeners(); - ~EventListeners(); + TestEventListeners(); + ~TestEventListeners(); // Appends an event listener to the end of the list. Google Test assumes // the ownership of the listener (i.e. it will delete the listener when @@ -871,8 +871,8 @@ class EventListeners { private: friend class TestCase; friend class internal::DefaultGlobalTestPartResultReporter; - friend class internal::EventListenersAccessor; friend class internal::NoExecDeathTest; + friend class internal::TestEventListenersAccessor; friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; @@ -906,8 +906,8 @@ class EventListeners { // Listener responsible for the creation of the XML output file. TestEventListener* default_xml_generator_; - // We disallow copying EventListeners. - GTEST_DISALLOW_COPY_AND_ASSIGN_(EventListeners); + // We disallow copying TestEventListeners. + GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners); }; // A UnitTest consists of a vector of TestCases. @@ -1002,7 +1002,7 @@ class UnitTest { // Returns the list of event listeners that can be used to track events // inside Google Test. - EventListeners& listeners(); + TestEventListeners& listeners(); private: // Registers and returns a global test environment. When a test -- cgit v1.2.3 From bd851333e89517762c91a3fef67cf25a6f1bd37a Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 30 Sep 2009 23:46:28 +0000 Subject: Implements test shuffling (by Zhanyong Wan, based on Josh Kelley's original patch). Enables death tests on minGW (by Vlad Losev). --- include/gtest/gtest.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 6fc5ac5c..9be15fbe 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -127,7 +127,7 @@ GTEST_DECLARE_int32_(repeat); // stack frames in failure stack traces. GTEST_DECLARE_bool_(show_internal_stack_frames); -// When this flag is specified, tests' order is randomized on every run. +// When this flag is specified, tests' order is randomized on every iteration. GTEST_DECLARE_bool_(shuffle); // This flag specifies the maximum number of stack frames to be @@ -675,6 +675,10 @@ class TestCase { return *test_info_list_; } + // Returns the i-th test among all the tests. i can range from 0 to + // total_test_count() - 1. If i is not in that range, returns NULL. + TestInfo* GetMutableTestInfo(int i); + // Sets the should_run member. void set_should_run(bool should) { should_run_ = should; } @@ -693,9 +697,6 @@ class TestCase { // Runs every test in this TestCase. void Run(); - // Runs every test in the given TestCase. - static void RunTestCase(TestCase * test_case) { test_case->Run(); } - // Returns true iff test passed. static bool TestPassed(const TestInfo * test_info); @@ -708,12 +709,23 @@ class TestCase { // Returns true if the given test should run. static bool ShouldRunTest(const TestInfo *test_info); + // Shuffles the tests in this test case. + void ShuffleTests(internal::Random* random); + + // Restores the test order to before the first shuffle. + void UnshuffleTests(); + // Name of the test case. internal::String name_; // Comment on the test case. internal::String comment_; - // Vector of TestInfos. - internal::Vector* test_info_list_; + // The vector of TestInfos in their original order. It owns the + // elements in the vector. + const internal::scoped_ptr > test_info_list_; + // Provides a level of indirection for the test list to allow easy + // shuffling and restoring the test order. The i-th element in this + // vector is the index of the i-th test in the shuffled test list. + const internal::scoped_ptr > test_indices_; // Pointer to the function that sets up the test case. Test::SetUpTestCaseFunc set_up_tc_; // Pointer to the function that tears down the test case. @@ -1030,6 +1042,10 @@ class UnitTest { // contains a property with the same key, the value will be updated. void RecordPropertyForCurrentTest(const char* key, const char* value); + // Gets the i-th test case among all the test cases. i can range from 0 to + // total_test_case_count() - 1. If i is not in that range, returns NULL. + TestCase* GetMutableTestCase(int i); + // Accessors for the implementation object. internal::UnitTestImpl* impl() { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; } -- cgit v1.2.3 From bad778caa39a88b7c11b159e20730aeec4fd711e Mon Sep 17 00:00:00 2001 From: vladlosev Date: Tue, 20 Oct 2009 21:03:10 +0000 Subject: Implements support for AssertionResult in Boolean assertions such as EXPECT_TRUE; Fixes Google Tests's tuple implementation to default-initialize its fields in the default constructor (by Zhanyong Wan); Populates gtest_stress_test.cc with actual tests. --- include/gtest/gtest.h | 146 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 115 insertions(+), 31 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 9be15fbe..33e2f7fe 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -177,63 +177,145 @@ String StreamableToString(const T& streamable) { // A class for indicating whether an assertion was successful. When // the assertion wasn't successful, the AssertionResult object -// remembers a non-empty message that described how it failed. +// remembers a non-empty message that describes how it failed. // -// This class is useful for defining predicate-format functions to be -// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). -// -// The constructor of AssertionResult is private. To create an -// instance of this class, use one of the factory functions +// To create an instance of this class, use one of the factory functions // (AssertionSuccess() and AssertionFailure()). // -// For example, in order to be able to write: +// This class is useful for two purposes: +// 1. Defining predicate functions to be used with Boolean test assertions +// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts +// 2. Defining predicate-format functions to be +// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). +// +// For example, if you define IsEven predicate: +// +// testing::AssertionResult IsEven(int n) { +// if ((n % 2) == 0) +// return testing::AssertionSuccess(); +// else +// return testing::AssertionFailure() << n << " is odd"; +// } +// +// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) +// will print the message +// +// Value of: IsEven(Fib(5)) +// Actual: false (5 is odd) +// Expected: true +// +// instead of a more opaque +// +// Value of: IsEven(Fib(5)) +// Actual: false +// Expected: true +// +// in case IsEven is a simple Boolean predicate. +// +// If you expect your predicate to be reused and want to support informative +// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up +// about half as often as positive ones in our tests), supply messages for +// both success and failure cases: +// +// testing::AssertionResult IsEven(int n) { +// if ((n % 2) == 0) +// return testing::AssertionSuccess() << n << " is even"; +// else +// return testing::AssertionFailure() << n << " is odd"; +// } +// +// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print +// +// Value of: IsEven(Fib(6)) +// Actual: true (8 is even) +// Expected: false +// +// NB: Predicates that support negative Boolean assertions have reduced +// performance in positive ones so be careful not to use them in tests +// that have lots (tens of thousands) of positive Boolean assertions. +// +// To use this class with EXPECT_PRED_FORMAT assertions such as: // // // Verifies that Foo() returns an even number. // EXPECT_PRED_FORMAT1(IsEven, Foo()); // -// you just need to define: +// you need to define: // // testing::AssertionResult IsEven(const char* expr, int n) { -// if ((n % 2) == 0) return testing::AssertionSuccess(); -// -// Message msg; -// msg << "Expected: " << expr << " is even\n" -// << " Actual: it's " << n; -// return testing::AssertionFailure(msg); +// if ((n % 2) == 0) +// return testing::AssertionSuccess(); +// else +// return testing::AssertionFailure() +// << "Expected: " << expr << " is even\n Actual: it's " << n; // } // // If Foo() returns 5, you will see the following message: // // Expected: Foo() is even // Actual: it's 5 +// class AssertionResult { public: - // Declares factory functions for making successful and failed - // assertion results as friends. - friend AssertionResult AssertionSuccess(); - friend AssertionResult AssertionFailure(const Message&); + // Copy constructor. + // Used in EXPECT_TRUE/FALSE(assertion_result). + AssertionResult(const AssertionResult& other); + // Used in the EXPECT_TRUE/FALSE(bool_expression). + explicit AssertionResult(bool success) : success_(success) {} // Returns true iff the assertion succeeded. - operator bool() const { return failure_message_.c_str() == NULL; } // NOLINT + operator bool() const { return success_; } // NOLINT + + // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. + AssertionResult operator!() const; + + // Returns the text streamed into this AssertionResult. Test assertions + // use it when they fail (i.e., the predicate's outcome doesn't match the + // assertion's expectation). When nothing has been streamed into the + // object, returns an empty string. + const char* message() const { + return message_.get() != NULL && message_->c_str() != NULL ? + message_->c_str() : ""; + } + // TODO(vladl@google.com): Remove this after making sure no clients use it. + // Deprecated; please use message() instead. + const char* failure_message() const { return message(); } - // Returns the assertion's failure message. - const char* failure_message() const { return failure_message_.c_str(); } + // Streams a custom failure message into this object. + template AssertionResult& operator<<(const T& value); private: - // The default constructor. It is used when the assertion succeeded. - AssertionResult() {} - - // The constructor used when the assertion failed. - explicit AssertionResult(const internal::String& failure_message); - - // Stores the assertion's failure message. - internal::String failure_message_; -}; + // No implementation - we want AssertionResult to be + // copy-constructible but not assignable. + void operator=(const AssertionResult& other); + + // Stores result of the assertion predicate. + bool success_; + // Stores the message describing the condition in case the expectation + // construct is not satisfied with the predicate's outcome. + // Referenced via a pointer to avoid taking too much stack frame space + // with test assertions. + internal::scoped_ptr message_; +}; // class AssertionResult + +// Streams a custom failure message into this object. +template +AssertionResult& AssertionResult::operator<<(const T& value) { + Message msg; + if (message_.get() != NULL) + msg << *message_; + msg << value; + message_.reset(new internal::String(msg.GetString())); + return *this; +} // Makes a successful assertion result. AssertionResult AssertionSuccess(); +// Makes a failed assertion result. +AssertionResult AssertionFailure(); + // Makes a failed assertion result with the given failure message. +// Deprecated; use AssertionFailure() << msg. AssertionResult AssertionFailure(const Message& msg); // The abstract class that all tests inherit from. @@ -1603,7 +1685,9 @@ const T* TestWithParam::parameter_ = NULL; #define ASSERT_ANY_THROW(statement) \ GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) -// Boolean assertions. +// Boolean assertions. Condition can be either a Boolean expression or an +// AssertionResult. For more information on how to use AssertionResult with +// these macros see comments on that class. #define EXPECT_TRUE(condition) \ GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ GTEST_NONFATAL_FAILURE_) -- cgit v1.2.3 From d56773b492b7b675d5c547baab815289a7815bdd Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 16 Dec 2009 19:54:05 +0000 Subject: Turns on -Wshadow (by Preston Jackson). --- include/gtest/gtest.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 33e2f7fe..c7df7f08 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -457,8 +457,8 @@ class TestProperty { // C'tor. TestProperty does NOT have a default constructor. // Always use this constructor (with parameters) to create a // TestProperty object. - TestProperty(const char* key, const char* value) : - key_(key), value_(value) { + TestProperty(const char* a_key, const char* a_value) : + key_(a_key), value_(a_value) { } // Gets the user supplied key. -- cgit v1.2.3 From 88e97c822c988eaa9f8bcbaa1ea5d702ffd7d384 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 16 Dec 2009 23:34:59 +0000 Subject: Removes uses of GTEST_HAS_STD_STRING. --- include/gtest/gtest.h | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index c7df7f08..02d4c5e8 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -62,24 +62,19 @@ #include // Depending on the platform, different string classes are available. -// On Windows, ::std::string compiles only when exceptions are -// enabled. On Linux, in addition to ::std::string, Google also makes -// use of class ::string, which has the same interface as -// ::std::string, but has a different implementation. -// -// The user can tell us whether ::std::string is available in his -// environment by defining the macro GTEST_HAS_STD_STRING to either 1 -// or 0 on the compiler command line. He can also define -// GTEST_HAS_GLOBAL_STRING to 1 to indicate that ::string is available -// AND is a distinct type to ::std::string, or define it to 0 to -// indicate otherwise. +// On Linux, in addition to ::std::string, Google also makes use of +// class ::string, which has the same interface as ::std::string, but +// has a different implementation. +// +// The user can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that +// ::string is available AND is a distinct type to ::std::string, or +// define it to 0 to indicate otherwise. // // If the user's ::std::string and ::string are the same class due to -// aliasing, he should define GTEST_HAS_STD_STRING to 1 and -// GTEST_HAS_GLOBAL_STRING to 0. +// aliasing, he should define GTEST_HAS_GLOBAL_STRING to 0. // -// If the user doesn't define GTEST_HAS_STD_STRING and/or -// GTEST_HAS_GLOBAL_STRING, they are defined heuristically. +// If the user doesn't define GTEST_HAS_GLOBAL_STRING, it is defined +// heuristically. namespace testing { @@ -1210,11 +1205,9 @@ void InitGoogleTest(int* argc, wchar_t** argv); namespace internal { // These overloaded versions handle ::std::string and ::std::wstring. -#if GTEST_HAS_STD_STRING inline String FormatForFailureMessage(const ::std::string& str) { return (Message() << '"' << str << '"').GetString(); } -#endif // GTEST_HAS_STD_STRING #if GTEST_HAS_STD_WSTRING inline String FormatForFailureMessage(const ::std::wstring& wstr) { @@ -1464,14 +1457,12 @@ AssertionResult IsNotSubstring( AssertionResult IsNotSubstring( const char* needle_expr, const char* haystack_expr, const wchar_t* needle, const wchar_t* haystack); -#if GTEST_HAS_STD_STRING AssertionResult IsSubstring( const char* needle_expr, const char* haystack_expr, const ::std::string& needle, const ::std::string& haystack); AssertionResult IsNotSubstring( const char* needle_expr, const char* haystack_expr, const ::std::string& needle, const ::std::string& haystack); -#endif // GTEST_HAS_STD_STRING #if GTEST_HAS_STD_WSTRING AssertionResult IsSubstring( -- cgit v1.2.3 From 0d27868d0faef474594682f25336229daa89d6d7 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 25 Feb 2010 01:09:07 +0000 Subject: Simplifies the implementation by using std::vector instead of Vector. --- include/gtest/gtest.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 02d4c5e8..c6f82e74 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -52,6 +52,8 @@ #define GTEST_INCLUDE_GTEST_GTEST_H_ #include +#include + #include #include #include @@ -535,13 +537,13 @@ class TestResult { friend class internal::WindowsDeathTest; // Gets the vector of TestPartResults. - const internal::Vector& test_part_results() const { - return *test_part_results_; + const std::vector& test_part_results() const { + return test_part_results_; } // Gets the vector of TestProperties. - const internal::Vector& test_properties() const { - return *test_properties_; + const std::vector& test_properties() const { + return test_properties_; } // Sets the elapsed time. @@ -579,9 +581,9 @@ class TestResult { internal::Mutex test_properites_mutex_; // The vector of TestPartResults - internal::scoped_ptr > test_part_results_; + std::vector test_part_results_; // The vector of TestProperties - internal::scoped_ptr > test_properties_; + std::vector test_properties_; // Running count of death tests. int death_test_count_; // The elapsed time, in milliseconds. @@ -745,11 +747,11 @@ class TestCase { friend class internal::UnitTestImpl; // Gets the (mutable) vector of TestInfos in this TestCase. - internal::Vector& test_info_list() { return *test_info_list_; } + std::vector& test_info_list() { return test_info_list_; } // Gets the (immutable) vector of TestInfos in this TestCase. - const internal::Vector & test_info_list() const { - return *test_info_list_; + const std::vector& test_info_list() const { + return test_info_list_; } // Returns the i-th test among all the tests. i can range from 0 to @@ -798,11 +800,11 @@ class TestCase { internal::String comment_; // The vector of TestInfos in their original order. It owns the // elements in the vector. - const internal::scoped_ptr > test_info_list_; + std::vector test_info_list_; // Provides a level of indirection for the test list to allow easy // shuffling and restoring the test order. The i-th element in this // vector is the index of the i-th test in the shuffled test list. - const internal::scoped_ptr > test_indices_; + std::vector test_indices_; // Pointer to the function that sets up the test case. Test::SetUpTestCaseFunc set_up_tc_; // Pointer to the function that tears down the test case. -- cgit v1.2.3 From 83589cca345d2f03d93b0555437aa480e0ed6699 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 5 Mar 2010 21:21:06 +0000 Subject: Supports building gtest as a DLL (by Vlad Losev). --- include/gtest/gtest.h | 102 +++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index c6f82e74..446f0281 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -251,7 +251,7 @@ String StreamableToString(const T& streamable) { // Expected: Foo() is even // Actual: it's 5 // -class AssertionResult { +class GTEST_API_ AssertionResult { public: // Copy constructor. // Used in EXPECT_TRUE/FALSE(assertion_result). @@ -306,14 +306,14 @@ AssertionResult& AssertionResult::operator<<(const T& value) { } // Makes a successful assertion result. -AssertionResult AssertionSuccess(); +GTEST_API_ AssertionResult AssertionSuccess(); // Makes a failed assertion result. -AssertionResult AssertionFailure(); +GTEST_API_ AssertionResult AssertionFailure(); // Makes a failed assertion result with the given failure message. // Deprecated; use AssertionFailure() << msg. -AssertionResult AssertionFailure(const Message& msg); +GTEST_API_ AssertionResult AssertionFailure(const Message& msg); // The abstract class that all tests inherit from. // @@ -338,7 +338,7 @@ AssertionResult AssertionFailure(const Message& msg); // TEST_F(FooTest, Baz) { ... } // // Test is not copyable. -class Test { +class GTEST_API_ Test { public: friend class internal::TestInfoImpl; @@ -486,7 +486,7 @@ class TestProperty { // the Test. // // TestResult is not copyable. -class TestResult { +class GTEST_API_ TestResult { public: // Creates an empty TestResult. TestResult(); @@ -604,7 +604,7 @@ class TestResult { // The constructor of TestInfo registers itself with the UnitTest // singleton such that the RUN_ALL_TESTS() macro knows which tests to // run. -class TestInfo { +class GTEST_API_ TestInfo { public: // Destructs a TestInfo object. This function is not virtual, so // don't inherit from TestInfo. @@ -686,7 +686,7 @@ class TestInfo { // A test case, which consists of a vector of TestInfos. // // TestCase is not copyable. -class TestCase { +class GTEST_API_ TestCase { public: // Creates a TestCase with the given name. // @@ -924,7 +924,7 @@ class EmptyTestEventListener : public TestEventListener { }; // TestEventListeners lets users add listeners to track events in Google Test. -class TestEventListeners { +class GTEST_API_ TestEventListeners { public: TestEventListeners(); ~TestEventListeners(); @@ -1011,7 +1011,7 @@ class TestEventListeners { // // This class is thread-safe as long as the methods are called // according to their specification. -class UnitTest { +class GTEST_API_ UnitTest { public: // Gets the singleton UnitTest object. The first time this method // is called, a UnitTest object is constructed and returned. @@ -1198,34 +1198,34 @@ inline Environment* AddGlobalTestEnvironment(Environment* env) { // updated. // // Calling the function for the second time has no user-visible effect. -void InitGoogleTest(int* argc, char** argv); +GTEST_API_ void InitGoogleTest(int* argc, char** argv); // This overloaded version can be used in Windows programs compiled in // UNICODE mode. -void InitGoogleTest(int* argc, wchar_t** argv); +GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); namespace internal { // These overloaded versions handle ::std::string and ::std::wstring. -inline String FormatForFailureMessage(const ::std::string& str) { +GTEST_API_ inline String FormatForFailureMessage(const ::std::string& str) { return (Message() << '"' << str << '"').GetString(); } #if GTEST_HAS_STD_WSTRING -inline String FormatForFailureMessage(const ::std::wstring& wstr) { +GTEST_API_ inline String FormatForFailureMessage(const ::std::wstring& wstr) { return (Message() << "L\"" << wstr << '"').GetString(); } #endif // GTEST_HAS_STD_WSTRING // These overloaded versions handle ::string and ::wstring. #if GTEST_HAS_GLOBAL_STRING -inline String FormatForFailureMessage(const ::string& str) { +GTEST_API_ inline String FormatForFailureMessage(const ::string& str) { return (Message() << '"' << str << '"').GetString(); } #endif // GTEST_HAS_GLOBAL_STRING #if GTEST_HAS_GLOBAL_WSTRING -inline String FormatForFailureMessage(const ::wstring& wstr) { +GTEST_API_ inline String FormatForFailureMessage(const ::wstring& wstr) { return (Message() << "L\"" << wstr << '"').GetString(); } #endif // GTEST_HAS_GLOBAL_WSTRING @@ -1391,51 +1391,51 @@ GTEST_IMPL_CMP_HELPER_(GT, > ) // The helper function for {ASSERT|EXPECT}_STREQ. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult CmpHelperSTREQ(const char* expected_expression, - const char* actual_expression, - const char* expected, - const char* actual); +GTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression, + const char* actual_expression, + const char* expected, + const char* actual); // The helper function for {ASSERT|EXPECT}_STRCASEEQ. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, - const char* actual_expression, - const char* expected, - const char* actual); +GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, + const char* actual_expression, + const char* expected, + const char* actual); // The helper function for {ASSERT|EXPECT}_STRNE. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult CmpHelperSTRNE(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2); +GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, + const char* s1, + const char* s2); // The helper function for {ASSERT|EXPECT}_STRCASENE. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult CmpHelperSTRCASENE(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2); +GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, + const char* s2_expression, + const char* s1, + const char* s2); // Helper function for *_STREQ on wide strings. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult CmpHelperSTREQ(const char* expected_expression, - const char* actual_expression, - const wchar_t* expected, - const wchar_t* actual); +GTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression, + const char* actual_expression, + const wchar_t* expected, + const wchar_t* actual); // Helper function for *_STRNE on wide strings. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult CmpHelperSTRNE(const char* s1_expression, - const char* s2_expression, - const wchar_t* s1, - const wchar_t* s2); +GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, + const wchar_t* s1, + const wchar_t* s2); } // namespace internal @@ -1513,16 +1513,16 @@ AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression, // Helper function for implementing ASSERT_NEAR. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -AssertionResult DoubleNearPredFormat(const char* expr1, - const char* expr2, - const char* abs_error_expr, - double val1, - double val2, - double abs_error); +GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, + const char* expr2, + const char* abs_error_expr, + double val1, + double val2, + double abs_error); // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. // A class that enables one to stream messages to assertion macros -class AssertHelper { +class GTEST_API_ AssertHelper { public: // Constructor. AssertHelper(TestPartResult::Type type, @@ -1853,10 +1853,10 @@ const T* TestWithParam::parameter_ = NULL; // Asserts that val1 is less than, or almost equal to, val2. Fails // otherwise. In particular, it fails if either val1 or val2 is NaN. -AssertionResult FloatLE(const char* expr1, const char* expr2, - float val1, float val2); -AssertionResult DoubleLE(const char* expr1, const char* expr2, - double val1, double val2); +GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, + float val1, float val2); +GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, + double val1, double val2); #if GTEST_OS_WINDOWS -- cgit v1.2.3 From 9f0824b0a61b50fd40e47da2387cd9b3f872ce47 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 22 Mar 2010 21:23:51 +0000 Subject: Adds missing gtest DLL exports. --- include/gtest/gtest.h | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 446f0281..ee7a8d56 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1278,10 +1278,10 @@ AssertionResult CmpHelperEQ(const char* expected_expression, // With this overloaded version, we allow anonymous enums to be used // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums // can be implicitly cast to BiggestInt. -AssertionResult CmpHelperEQ(const char* expected_expression, - const char* actual_expression, - BiggestInt expected, - BiggestInt actual); +GTEST_API_ AssertionResult CmpHelperEQ(const char* expected_expression, + const char* actual_expression, + BiggestInt expected, + BiggestInt actual); // The helper class for {ASSERT|EXPECT}_EQ. The template argument // lhs_is_null_literal is true iff the first argument to ASSERT_EQ() @@ -1370,21 +1370,21 @@ AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ return AssertionFailure(msg);\ }\ }\ -AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ - BiggestInt val1, BiggestInt val2); +GTEST_API_ AssertionResult CmpHelper##op_name(\ + const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2) // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. // Implements the helper function for {ASSERT|EXPECT}_NE -GTEST_IMPL_CMP_HELPER_(NE, !=) +GTEST_IMPL_CMP_HELPER_(NE, !=); // Implements the helper function for {ASSERT|EXPECT}_LE -GTEST_IMPL_CMP_HELPER_(LE, <=) +GTEST_IMPL_CMP_HELPER_(LE, <=); // Implements the helper function for {ASSERT|EXPECT}_LT -GTEST_IMPL_CMP_HELPER_(LT, < ) +GTEST_IMPL_CMP_HELPER_(LT, < ); // Implements the helper function for {ASSERT|EXPECT}_GE -GTEST_IMPL_CMP_HELPER_(GE, >=) +GTEST_IMPL_CMP_HELPER_(GE, >=); // Implements the helper function for {ASSERT|EXPECT}_GT -GTEST_IMPL_CMP_HELPER_(GT, > ) +GTEST_IMPL_CMP_HELPER_(GT, > ); #undef GTEST_IMPL_CMP_HELPER_ @@ -1447,30 +1447,30 @@ GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, // // The {needle,haystack}_expr arguments are the stringified // expressions that generated the two real arguments. -AssertionResult IsSubstring( +GTEST_API_ AssertionResult IsSubstring( const char* needle_expr, const char* haystack_expr, const char* needle, const char* haystack); -AssertionResult IsSubstring( +GTEST_API_ AssertionResult IsSubstring( const char* needle_expr, const char* haystack_expr, const wchar_t* needle, const wchar_t* haystack); -AssertionResult IsNotSubstring( +GTEST_API_ AssertionResult IsNotSubstring( const char* needle_expr, const char* haystack_expr, const char* needle, const char* haystack); -AssertionResult IsNotSubstring( +GTEST_API_ AssertionResult IsNotSubstring( const char* needle_expr, const char* haystack_expr, const wchar_t* needle, const wchar_t* haystack); -AssertionResult IsSubstring( +GTEST_API_ AssertionResult IsSubstring( const char* needle_expr, const char* haystack_expr, const ::std::string& needle, const ::std::string& haystack); -AssertionResult IsNotSubstring( +GTEST_API_ AssertionResult IsNotSubstring( const char* needle_expr, const char* haystack_expr, const ::std::string& needle, const ::std::string& haystack); #if GTEST_HAS_STD_WSTRING -AssertionResult IsSubstring( +GTEST_API_ AssertionResult IsSubstring( const char* needle_expr, const char* haystack_expr, const ::std::wstring& needle, const ::std::wstring& haystack); -AssertionResult IsNotSubstring( +GTEST_API_ AssertionResult IsNotSubstring( const char* needle_expr, const char* haystack_expr, const ::std::wstring& needle, const ::std::wstring& haystack); #endif // GTEST_HAS_STD_WSTRING -- cgit v1.2.3 From 1b71f0b272f4d3dfb45db44ab39a77727ddafb9b Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 13 Apr 2010 04:40:32 +0000 Subject: Adds alternative spellings for FAIL, SUCCEED, and TEST. --- include/gtest/gtest.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index ee7a8d56..921fad11 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1651,10 +1651,22 @@ const T* TestWithParam::parameter_ = NULL; #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") // Generates a fatal failure with a generic message. -#define FAIL() GTEST_FATAL_FAILURE_("Failed") +#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") + +// Define this macro to 1 to omit the definition of FAIL(), which is a +// generic name and clashes with some other libraries. +#if !GTEST_DONT_DEFINE_FAIL +#define FAIL() GTEST_FAIL() +#endif // Generates a success with a generic message. -#define SUCCEED() GTEST_SUCCESS_("Succeeded") +#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") + +// Define this macro to 1 to omit the definition of SUCCEED(), which +// is a generic name and clashes with some other libraries. +#if !GTEST_DONT_DEFINE_SUCCEED +#define SUCCEED() GTEST_SUCCEED() +#endif // Macros for testing exceptions. // @@ -1986,10 +1998,15 @@ bool StaticAssertTypeEq() { // code. GetTestTypeId() is guaranteed to always return the same // value, as it always calls GetTypeId<>() from the Google Test // framework. -#define TEST(test_case_name, test_name)\ +#define GTEST_TEST(test_case_name, test_name)\ GTEST_TEST_(test_case_name, test_name, \ ::testing::Test, ::testing::internal::GetTestTypeId()) +// Define this macro to 1 to omit the definition of TEST(), which +// is a generic name and clashes with some other libraries. +#if !GTEST_DONT_DEFINE_TEST +#define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name) +#endif // Defines a test that uses a test fixture. // -- cgit v1.2.3 From cdc0aae155e9069eeebcb191570b8cb1b258ecd8 Mon Sep 17 00:00:00 2001 From: chandlerc Date: Sun, 9 May 2010 08:16:50 +0000 Subject: Silence a Clang warning about an unused variable. --- include/gtest/gtest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 921fad11..d0277246 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1969,7 +1969,7 @@ struct StaticAssertTypeEqHelper {}; // to cause a compiler error. template bool StaticAssertTypeEq() { - internal::StaticAssertTypeEqHelper(); + (void)internal::StaticAssertTypeEqHelper(); return true; } -- cgit v1.2.3 From 2ccea88c99d1ae23383d1b8eb3680a4a4d2edd66 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 10 May 2010 17:11:58 +0000 Subject: Moves the universal printer from gmock to gtest and refactors the cmake script for reusing in gmock (by Vlad Losev). --- include/gtest/gtest.h | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index d0277246..4599aba1 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -1926,17 +1927,6 @@ GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ __FILE__, __LINE__, ::testing::Message() << (message)) -namespace internal { - -// This template is declared, but intentionally undefined. -template -struct StaticAssertTypeEqHelper; - -template -struct StaticAssertTypeEqHelper {}; - -} // namespace internal - // Compile-time assertion for type equality. // StaticAssertTypeEq() compiles iff type1 and type2 are // the same type. The value it returns is not interesting. @@ -1969,7 +1959,7 @@ struct StaticAssertTypeEqHelper {}; // to cause a compiler error. template bool StaticAssertTypeEq() { - (void)internal::StaticAssertTypeEqHelper(); + internal::StaticAssertTypeEqHelper(); return true; } -- cgit v1.2.3 From 61baf319bbb928707a21d78d7ad8fd0bd04e714d Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 10 May 2010 17:23:54 +0000 Subject: Suppresses some Clang warnings (by Chandler Carruth, Jeffrey Yasskin, and Zhanyong Wan). --- include/gtest/gtest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 4599aba1..937f4761 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1959,7 +1959,7 @@ GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, // to cause a compiler error. template bool StaticAssertTypeEq() { - internal::StaticAssertTypeEqHelper(); + (void)internal::StaticAssertTypeEqHelper(); return true; } -- cgit v1.2.3 From 38e1465902692b70ed11f670c8d335dbded5522f Mon Sep 17 00:00:00 2001 From: vladlosev Date: Mon, 31 May 2010 23:30:01 +0000 Subject: Fixes a wrong comment for OnTestPartResult(). --- include/gtest/gtest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 937f4761..867f2a72 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -877,7 +877,7 @@ class TestEventListener { // Fired before the test starts. virtual void OnTestStart(const TestInfo& test_info) = 0; - // Fired after a failed assertion or a SUCCESS(). + // Fired after a failed assertion or a SUCCEED() invocation. virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; // Fired after the test ends. -- cgit v1.2.3 From e2a7f03b80fc0e9e6a6f36acb43776509486a6d4 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 21 Jul 2010 22:15:17 +0000 Subject: Allows EXPECT_EQ to accept arguments that don't have operator << (by Zhanyong Wan). Allows a user to customize how the universal printer prints a pointer of a specific type by overloading << (by Zhanyong Wan). Works around a bug in Cymbian's C++ compiler (by Vlad Losev). --- include/gtest/gtest.h | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 867f2a72..90dde389 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1207,30 +1207,6 @@ GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); namespace internal { -// These overloaded versions handle ::std::string and ::std::wstring. -GTEST_API_ inline String FormatForFailureMessage(const ::std::string& str) { - return (Message() << '"' << str << '"').GetString(); -} - -#if GTEST_HAS_STD_WSTRING -GTEST_API_ inline String FormatForFailureMessage(const ::std::wstring& wstr) { - return (Message() << "L\"" << wstr << '"').GetString(); -} -#endif // GTEST_HAS_STD_WSTRING - -// These overloaded versions handle ::string and ::wstring. -#if GTEST_HAS_GLOBAL_STRING -GTEST_API_ inline String FormatForFailureMessage(const ::string& str) { - return (Message() << '"' << str << '"').GetString(); -} -#endif // GTEST_HAS_GLOBAL_STRING - -#if GTEST_HAS_GLOBAL_WSTRING -GTEST_API_ inline String FormatForFailureMessage(const ::wstring& wstr) { - return (Message() << "L\"" << wstr << '"').GetString(); -} -#endif // GTEST_HAS_GLOBAL_WSTRING - // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) // operand to be used in a failure message. The type (but not value) // of the other operand may affect the format. This allows us to @@ -1246,7 +1222,7 @@ GTEST_API_ inline String FormatForFailureMessage(const ::wstring& wstr) { template String FormatForComparisonFailureMessage(const T1& value, const T2& /* other_operand */) { - return FormatForFailureMessage(value); + return PrintToString(value); } // The helper function for {ASSERT|EXPECT}_EQ. -- cgit v1.2.3 From 7c598c4f1a44fdda5f97587484c85bef9e93fa98 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 26 Jul 2010 21:59:50 +0000 Subject: Adds ADD_FAILURE_AT (by Zhanyong Wan); disables -Wswitch-default (by Vlad Losev). --- include/gtest/gtest.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 90dde389..af9d9e7c 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1627,6 +1627,12 @@ const T* TestWithParam::parameter_ = NULL; // Generates a nonfatal failure with a generic message. #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") +// Generates a nonfatal failure at the given source file location with +// a generic message. +#define ADD_FAILURE_AT(file, line) \ + GTEST_MESSAGE_AT_(file, line, "Failed", \ + ::testing::TestPartResult::kNonFatalFailure) + // Generates a fatal failure with a generic message. #define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") -- cgit v1.2.3 From 5c4b472bbf8c81fc3d52fc69a92f174821a96280 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 9 Aug 2010 18:19:15 +0000 Subject: Makes gtest print enums as integers instead of hex dumps (by Zhanyong Wan); improves the hex dump format (by Zhanyong Wan); gets rid of class TestInfoImpl (by Zhanyong Wan); adds exception handling (by Vlad Losev). --- include/gtest/gtest.h | 90 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 28 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index af9d9e7c..20028a17 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -148,7 +148,6 @@ class ExecDeathTest; class NoExecDeathTest; class FinalSuccessChecker; class GTestFlagSaver; -class TestInfoImpl; class TestResultAccessor; class TestEventListenersAccessor; class TestEventRepeater; @@ -341,7 +340,7 @@ GTEST_API_ AssertionResult AssertionFailure(const Message& msg); // Test is not copyable. class GTEST_API_ Test { public: - friend class internal::TestInfoImpl; + friend class TestInfo; // Defines types for pointers to functions that set up and tear down // a test case. @@ -418,6 +417,10 @@ class GTEST_API_ Test { // Sets up, executes, and tears down the test. void Run(); + // Deletes self. We deliberately pick an unusual name for this + // internal method to avoid clashing with names used in user TESTs. + void DeleteSelf_() { delete this; } + // Uses a GTestFlagSaver to save and restore all Google Test flags. const internal::GTestFlagSaver* const gtest_flag_saver_; @@ -532,7 +535,6 @@ class GTEST_API_ TestResult { friend class UnitTest; friend class internal::DefaultGlobalTestPartResultReporter; friend class internal::ExecDeathTest; - friend class internal::TestInfoImpl; friend class internal::TestResultAccessor; friend class internal::UnitTestImpl; friend class internal::WindowsDeathTest; @@ -612,16 +614,16 @@ class GTEST_API_ TestInfo { ~TestInfo(); // Returns the test case name. - const char* test_case_name() const; + const char* test_case_name() const { return test_case_name_.c_str(); } // Returns the test name. - const char* name() const; + const char* name() const { return name_.c_str(); } // Returns the test case comment. - const char* test_case_comment() const; + const char* test_case_comment() const { return test_case_comment_.c_str(); } // Returns the test comment. - const char* comment() const; + const char* comment() const { return comment_.c_str(); } // Returns true if this test should run, that is if the test is not disabled // (or it is disabled but the also_run_disabled_tests flag has been specified) @@ -639,10 +641,10 @@ class GTEST_API_ TestInfo { // // For example, *A*:Foo.* is a filter that matches any string that // contains the character 'A' or starts with "Foo.". - bool should_run() const; + bool should_run() const { return should_run_; } // Returns the result of the test. - const TestResult* result() const; + const TestResult* result() const { return &result_; } private: #if GTEST_HAS_DEATH_TEST @@ -650,7 +652,6 @@ class GTEST_API_ TestInfo { #endif // GTEST_HAS_DEATH_TEST friend class Test; friend class TestCase; - friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; friend TestInfo* internal::MakeAndRegisterTestInfo( const char* test_case_name, const char* name, @@ -660,17 +661,6 @@ class GTEST_API_ TestInfo { Test::TearDownTestCaseFunc tear_down_tc, internal::TestFactoryBase* factory); - // Returns true if this test matches the user-specified filter. - bool matches_filter() const; - - // Increments the number of death tests encountered in this test so - // far. - int increment_death_test_count(); - - // Accessors for the implementation object. - internal::TestInfoImpl* impl() { return impl_; } - const internal::TestInfoImpl* impl() const { return impl_; } - // Constructs a TestInfo object. The newly constructed instance assumes // ownership of the factory object. TestInfo(const char* test_case_name, const char* name, @@ -678,8 +668,36 @@ class GTEST_API_ TestInfo { internal::TypeId fixture_class_id, internal::TestFactoryBase* factory); - // An opaque implementation object. - internal::TestInfoImpl* impl_; + // Increments the number of death tests encountered in this test so + // far. + int increment_death_test_count() { + return result_.increment_death_test_count(); + } + + // Creates the test object, runs it, records its result, and then + // deletes it. + void Run(); + + static void ClearTestResult(TestInfo* test_info) { + test_info->result_.Clear(); + } + + // These fields are immutable properties of the test. + const std::string test_case_name_; // Test case name + const std::string name_; // Test name + const std::string test_case_comment_; // Test case comment + const std::string comment_; // Test comment + const internal::TypeId fixture_class_id_; // ID of the test fixture class + bool should_run_; // True iff this test should run + bool is_disabled_; // True iff this test is disabled + bool matches_filter_; // True if this test matches the + // user-specified filter. + internal::TestFactoryBase* const factory_; // The factory that creates + // the test object + + // This field is mutable and needs to be reset before running the + // test for the second time. + TestResult result_; GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); }; @@ -777,17 +795,33 @@ class GTEST_API_ TestCase { // Runs every test in this TestCase. void Run(); + // Runs SetUpTestCase() for this TestCase. This wrapper is needed + // for catching exceptions thrown from SetUpTestCase(). + void RunSetUpTestCase() { (*set_up_tc_)(); } + + // Runs TearDownTestCase() for this TestCase. This wrapper is + // needed for catching exceptions thrown from TearDownTestCase(). + void RunTearDownTestCase() { (*tear_down_tc_)(); } + // Returns true iff test passed. - static bool TestPassed(const TestInfo * test_info); + static bool TestPassed(const TestInfo* test_info) { + return test_info->should_run() && test_info->result()->Passed(); + } // Returns true iff test failed. - static bool TestFailed(const TestInfo * test_info); + static bool TestFailed(const TestInfo* test_info) { + return test_info->should_run() && test_info->result()->Failed(); + } // Returns true iff test is disabled. - static bool TestDisabled(const TestInfo * test_info); + static bool TestDisabled(const TestInfo* test_info) { + return test_info->is_disabled_; + } // Returns true if the given test should run. - static bool ShouldRunTest(const TestInfo *test_info); + static bool ShouldRunTest(const TestInfo* test_info) { + return test_info->should_run(); + } // Shuffles the tests in this test case. void ShuffleTests(internal::Random* random); @@ -962,10 +996,10 @@ class GTEST_API_ TestEventListeners { private: friend class TestCase; + friend class TestInfo; friend class internal::DefaultGlobalTestPartResultReporter; friend class internal::NoExecDeathTest; friend class internal::TestEventListenersAccessor; - friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; // Returns repeater that broadcasts the TestEventListener events to all -- cgit v1.2.3 From a9f380f5c7ff75cd715c58e11885dddc54baef02 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 19 Aug 2010 22:16:00 +0000 Subject: Removes the Windows golden file (by Vlad Losev); implements test result streaming (by Nikhil Jindal and cleaned up by Zhanyong Wan). --- include/gtest/gtest.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 20028a17..3efbecef 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -137,6 +137,11 @@ GTEST_DECLARE_int32_(stack_trace_depth); // non-zero code otherwise. GTEST_DECLARE_bool_(throw_on_failure); +// When this flag is set with a "host:port" string, on supported +// platforms test results are streamed to the specified port on +// the specified host machine. +GTEST_DECLARE_string_(stream_result_to); + // The upper limit for valid stack trace depths. const int kMaxStackTraceDepth = 100; @@ -155,8 +160,6 @@ class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResult::Type result_type, const String& message); -class PrettyUnitTestResultPrinter; -class XmlUnitTestResultPrinter; // Converts a streamable value to a String. A NULL pointer is // converted to "(null)". When the input value is a ::string, -- cgit v1.2.3 From 35c39756495bea5959de5778aaaf33a94f8c1e2e Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 31 Aug 2010 18:21:13 +0000 Subject: Casts char to unsigned char before calling isspace() etc to avoid undefined behavior (by Zhanyong Wan); removes conditional #includes keyed on GTEST_HAS_PROTOBUF_ (by Zhanyong Wan); publishes GTEST_HAS_STREAM_REDIRECTION (by Vlad Losev); forward declares some classes properly (by Samuel Benzaquen); honors the --gtest_catch_exceptions flag (by Vlad Losev). --- include/gtest/gtest.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 3efbecef..73f05781 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -175,6 +175,14 @@ String StreamableToString(const T& streamable) { } // namespace internal +// The friend relationship of some of these classes is cyclic. +// If we don't forward declare them the compiler might confuse the classes +// in friendship clauses with same named classes on the scope. +class Test; +class TestCase; +class TestInfo; +class UnitTest; + // A class for indicating whether an assertion was successful. When // the assertion wasn't successful, the AssertionResult object // remembers a non-empty message that describes how it failed. -- cgit v1.2.3 From 88e0df62470fa82e8d3010ef88241cd7565ebe9e Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 8 Sep 2010 05:57:37 +0000 Subject: Removes all uses of StrStream; fixes the VC projects and simplifies them by using gtest-all.cc. --- include/gtest/gtest.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 73f05781..41d27965 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1517,18 +1517,18 @@ AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression, return AssertionSuccess(); } - StrStream expected_ss; + ::std::stringstream expected_ss; expected_ss << std::setprecision(std::numeric_limits::digits10 + 2) << expected; - StrStream actual_ss; + ::std::stringstream actual_ss; actual_ss << std::setprecision(std::numeric_limits::digits10 + 2) << actual; return EqFailure(expected_expression, actual_expression, - StrStreamToString(&expected_ss), - StrStreamToString(&actual_ss), + StringStreamToString(&expected_ss), + StringStreamToString(&actual_ss), false); } -- cgit v1.2.3 From dac3e879c56a50696a36f53e1e5e353e48fa665f Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 14 Sep 2010 05:35:59 +0000 Subject: Include gtest headers as user headers instead of system headers. --- include/gtest/gtest.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 41d27965..334a52d1 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -54,15 +54,15 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-string.h" +#include "gtest/gtest-death-test.h" +#include "gtest/gtest-message.h" +#include "gtest/gtest-param-test.h" +#include "gtest/gtest-printers.h" +#include "gtest/gtest_prod.h" +#include "gtest/gtest-test-part.h" +#include "gtest/gtest-typed-test.h" // Depending on the platform, different string classes are available. // On Linux, in addition to ::std::string, Google also makes use of @@ -1736,7 +1736,7 @@ const T* TestWithParam::parameter_ = NULL; // Includes the auto-generated header that implements a family of // generic predicate assertion macros. -#include +#include "gtest/gtest_pred_impl.h" // Macros for testing equalities and inequalities. // -- cgit v1.2.3 From 345d9ebf30ccc2747c4e4c224b95fd8406093e29 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 15 Sep 2010 04:56:58 +0000 Subject: Implements GTEST_ASSERT_XY as alias of ASSERT_XY. --- include/gtest/gtest.h | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 334a52d1..71eccac2 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1799,21 +1799,48 @@ const T* TestWithParam::parameter_ = NULL; #define EXPECT_GT(val1, val2) \ EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) -#define ASSERT_EQ(expected, actual) \ +#define GTEST_ASSERT_EQ(expected, actual) \ ASSERT_PRED_FORMAT2(::testing::internal:: \ EqHelper::Compare, \ expected, actual) -#define ASSERT_NE(val1, val2) \ +#define GTEST_ASSERT_NE(val1, val2) \ ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) -#define ASSERT_LE(val1, val2) \ +#define GTEST_ASSERT_LE(val1, val2) \ ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) -#define ASSERT_LT(val1, val2) \ +#define GTEST_ASSERT_LT(val1, val2) \ ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) -#define ASSERT_GE(val1, val2) \ +#define GTEST_ASSERT_GE(val1, val2) \ ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) -#define ASSERT_GT(val1, val2) \ +#define GTEST_ASSERT_GT(val1, val2) \ ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) +// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of +// ASSERT_XY(), which clashes with some users' own code. + +#if !GTEST_DONT_DEFINE_ASSERT_EQ +#define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_NE +#define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_LE +#define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_LT +#define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_GE +#define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_GT +#define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) +#endif + // C String Comparisons. All tests treat NULL and any non-NULL string // as different. Two NULLs are equal. // -- cgit v1.2.3 From 2d1835b086e69570e4c3e0ad6197da509bd0a957 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 27 Sep 2010 22:09:42 +0000 Subject: Removes uses of deprecated AssertionFailure() API (by Vlad Losev). --- include/gtest/gtest.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 71eccac2..c725e4cc 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1385,11 +1385,10 @@ AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ if (val1 op val2) {\ return AssertionSuccess();\ } else {\ - Message msg;\ - msg << "Expected: (" << expr1 << ") " #op " (" << expr2\ + return AssertionFailure() \ + << "Expected: (" << expr1 << ") " #op " (" << expr2\ << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ << " vs " << FormatForComparisonFailureMessage(val2, val1);\ - return AssertionFailure(msg);\ }\ }\ GTEST_API_ AssertionResult CmpHelper##op_name(\ -- cgit v1.2.3 From 25958f3e4c4097caca8347b7937f5f6fb26d6c56 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Fri, 22 Oct 2010 01:33:11 +0000 Subject: Fixes compiler warning when built with -std=c++0x. --- include/gtest/gtest.h | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index c725e4cc..6ce58d72 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -281,20 +281,33 @@ class GTEST_API_ AssertionResult { // assertion's expectation). When nothing has been streamed into the // object, returns an empty string. const char* message() const { - return message_.get() != NULL && message_->c_str() != NULL ? - message_->c_str() : ""; + return message_.get() != NULL ? message_->c_str() : ""; } // TODO(vladl@google.com): Remove this after making sure no clients use it. // Deprecated; please use message() instead. const char* failure_message() const { return message(); } // Streams a custom failure message into this object. - template AssertionResult& operator<<(const T& value); + template AssertionResult& operator<<(const T& value) { + AppendMessage(Message() << value); + return *this; + } + + // Allows streaming basic output manipulators such as endl or flush into + // this object. + AssertionResult& operator<<( + ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { + AppendMessage(Message() << basic_manipulator); + return *this; + } private: - // No implementation - we want AssertionResult to be - // copy-constructible but not assignable. - void operator=(const AssertionResult& other); + // Appends the contents of message to message_. + void AppendMessage(const Message& a_message) { + if (message_.get() == NULL) + message_.reset(new ::std::string); + message_->append(a_message.GetString().c_str()); + } // Stores result of the assertion predicate. bool success_; @@ -302,19 +315,10 @@ class GTEST_API_ AssertionResult { // construct is not satisfied with the predicate's outcome. // Referenced via a pointer to avoid taking too much stack frame space // with test assertions. - internal::scoped_ptr message_; -}; // class AssertionResult + internal::scoped_ptr< ::std::string> message_; -// Streams a custom failure message into this object. -template -AssertionResult& AssertionResult::operator<<(const T& value) { - Message msg; - if (message_.get() != NULL) - msg << *message_; - msg << value; - message_.reset(new internal::String(msg.GetString())); - return *this; -} + GTEST_DISALLOW_ASSIGN_(AssertionResult); +}; // Makes a successful assertion result. GTEST_API_ AssertionResult AssertionSuccess(); -- cgit v1.2.3 From 915129ee6fd4d9c5564aafedb238a237592a3d42 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 6 Dec 2010 22:18:59 +0000 Subject: Allows a value-parameterized test fixture to derive from Test and WithParamInterface separately; contributed by Matt Austern. --- include/gtest/gtest.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 6ce58d72..f7ed948b 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1589,9 +1589,13 @@ class GTEST_API_ AssertHelper { } // namespace internal #if GTEST_HAS_PARAM_TEST -// The abstract base class that all value-parameterized tests inherit from. +// The pure interface class that all value-parameterized tests inherit from. +// A value-parameterized class must inherit from both ::testing::Test and +// ::testing::WithParamInterface. In most cases that just means inheriting +// from ::testing::TestWithParam, but more complicated test hierarchies +// may need to inherit from Test and WithParamInterface at different levels. // -// This class adds support for accessing the test parameter value via +// This interface has support for accessing the test parameter value via // the GetParam() method. // // Use it with one of the parameter generator defining functions, like Range(), @@ -1620,12 +1624,16 @@ class GTEST_API_ AssertHelper { // INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); template -class TestWithParam : public Test { +class WithParamInterface { public: typedef T ParamType; + virtual ~WithParamInterface() {} // The current parameter value. Is also available in the test fixture's - // constructor. + // constructor. This member function is non-static, even though it only + // references static data, to reduce the opportunity for incorrect uses + // like writing 'WithParamInterface::GetParam()' for a test that + // uses a fixture whose parameter type is int. const ParamType& GetParam() const { return *parameter_; } private: @@ -1638,12 +1646,19 @@ class TestWithParam : public Test { // Static value used for accessing parameter during a test lifetime. static const ParamType* parameter_; - // TestClass must be a subclass of TestWithParam. + // TestClass must be a subclass of WithParamInterface and Test. template friend class internal::ParameterizedTestFactory; }; template -const T* TestWithParam::parameter_ = NULL; +const T* WithParamInterface::parameter_ = NULL; + +// Most value-parameterized classes can ignore the existence of +// WithParamInterface, and can just inherit from ::testing::TestWithParam. + +template +class TestWithParam : public Test, public WithParamInterface { +}; #endif // GTEST_HAS_PARAM_TEST -- cgit v1.2.3 From 48b1315108cdba8f37394aedb8098102ca00e8b9 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 10 Jan 2011 18:17:59 +0000 Subject: Fixes GCC 4.6 warnings (patch by Jeffrey Yasskin). --- include/gtest/gtest.h | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index f7ed948b..79e604ca 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1342,7 +1342,7 @@ class EqHelper { }; // This specialization is used when the first argument to ASSERT_EQ() -// is a null pointer literal. +// is a null pointer literal, like NULL, false, or 0. template <> class EqHelper { public: @@ -1351,24 +1351,38 @@ class EqHelper { // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or // EXPECT_EQ(false, a_bool). template - static AssertionResult Compare(const char* expected_expression, - const char* actual_expression, - const T1& expected, - const T2& actual) { + static AssertionResult Compare( + const char* expected_expression, + const char* actual_expression, + const T1& expected, + const T2& actual, + // The following line prevents this overload from being considered if T2 + // is not a pointer type. We need this because ASSERT_EQ(NULL, my_ptr) + // expands to Compare("", "", NULL, my_ptr), which requires a conversion + // to match the Secret* in the other overload, which would otherwise make + // this template match better. + typename EnableIf::value>::type* = 0) { return CmpHelperEQ(expected_expression, actual_expression, expected, actual); } - // This version will be picked when the second argument to - // ASSERT_EQ() is a pointer, e.g. ASSERT_EQ(NULL, a_pointer). - template - static AssertionResult Compare(const char* expected_expression, - const char* actual_expression, - const T1& /* expected */, - T2* actual) { + // This version will be picked when the second argument to ASSERT_EQ() is a + // pointer, e.g. ASSERT_EQ(NULL, a_pointer). + template + static AssertionResult Compare( + const char* expected_expression, + const char* actual_expression, + // We used to have a second template parameter instead of Secret*. That + // template parameter would deduce to 'long', making this a better match + // than the first overload even without the first overload's EnableIf. + // Unfortunately, gcc with -Wconversion-null warns when "passing NULL to + // non-pointer argument" (even a deduced integral argument), so the old + // implementation caused warnings in user code. + Secret* /* expected (NULL) */, + T* actual) { // We already know that 'expected' is a null pointer. return CmpHelperEQ(expected_expression, actual_expression, - static_cast(NULL), actual); + static_cast(NULL), actual); } }; -- cgit v1.2.3 From 9bcf4d0a654b27732e2fa901fe98c09aba71773a Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 2 Feb 2011 00:49:33 +0000 Subject: Adds type_param and value_param as attributes to the XML report; also removes the comment() and test_case_comment() fields of TestInfo. Proposed and initally implemented by Joey Oravec. Re-implemented by Vlad Losev. --- include/gtest/gtest.h | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 79e604ca..741a3607 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -634,11 +634,21 @@ class GTEST_API_ TestInfo { // Returns the test name. const char* name() const { return name_.c_str(); } - // Returns the test case comment. - const char* test_case_comment() const { return test_case_comment_.c_str(); } + // Returns the name of the parameter type, or NULL if this is not a typed + // or a type-parameterized test. + const char* type_param() const { + if (type_param_.get() != NULL) + return type_param_->c_str(); + return NULL; + } - // Returns the test comment. - const char* comment() const { return comment_.c_str(); } + // Returns the text representation of the value parameter, or NULL if this + // is not a value-parameterized test. + const char* value_param() const { + if (value_param_.get() != NULL) + return value_param_->c_str(); + return NULL; + } // Returns true if this test should run, that is if the test is not disabled // (or it is disabled but the also_run_disabled_tests flag has been specified) @@ -670,7 +680,8 @@ class GTEST_API_ TestInfo { friend class internal::UnitTestImpl; friend TestInfo* internal::MakeAndRegisterTestInfo( const char* test_case_name, const char* name, - const char* test_case_comment, const char* comment, + const char* type_param, + const char* value_param, internal::TypeId fixture_class_id, Test::SetUpTestCaseFunc set_up_tc, Test::TearDownTestCaseFunc tear_down_tc, @@ -679,7 +690,8 @@ class GTEST_API_ TestInfo { // Constructs a TestInfo object. The newly constructed instance assumes // ownership of the factory object. TestInfo(const char* test_case_name, const char* name, - const char* test_case_comment, const char* comment, + const char* a_type_param, + const char* a_value_param, internal::TypeId fixture_class_id, internal::TestFactoryBase* factory); @@ -700,8 +712,12 @@ class GTEST_API_ TestInfo { // These fields are immutable properties of the test. const std::string test_case_name_; // Test case name const std::string name_; // Test name - const std::string test_case_comment_; // Test case comment - const std::string comment_; // Test comment + // Name of the parameter type, or NULL if this is not a typed or a + // type-parameterized test. + const internal::scoped_ptr type_param_; + // Text representation of the value parameter, or NULL if this is not a + // value-parameterized test. + const internal::scoped_ptr value_param_; const internal::TypeId fixture_class_id_; // ID of the test fixture class bool should_run_; // True iff this test should run bool is_disabled_; // True iff this test is disabled @@ -730,9 +746,11 @@ class GTEST_API_ TestCase { // Arguments: // // name: name of the test case + // a_type_param: the name of the test's type parameter, or NULL if + // this is not a type-parameterized test. // set_up_tc: pointer to the function that sets up the test case // tear_down_tc: pointer to the function that tears down the test case - TestCase(const char* name, const char* comment, + TestCase(const char* name, const char* a_type_param, Test::SetUpTestCaseFunc set_up_tc, Test::TearDownTestCaseFunc tear_down_tc); @@ -742,8 +760,13 @@ class GTEST_API_ TestCase { // Gets the name of the TestCase. const char* name() const { return name_.c_str(); } - // Returns the test case comment. - const char* comment() const { return comment_.c_str(); } + // Returns the name of the parameter type, or NULL if this is not a + // type-parameterized test case. + const char* type_param() const { + if (type_param_.get() != NULL) + return type_param_->c_str(); + return NULL; + } // Returns true if any test in this test case should run. bool should_run() const { return should_run_; } @@ -846,8 +869,9 @@ class GTEST_API_ TestCase { // Name of the test case. internal::String name_; - // Comment on the test case. - internal::String comment_; + // Name of the parameter type, or NULL if this is not a typed or a + // type-parameterized test. + const internal::scoped_ptr type_param_; // The vector of TestInfos in their original order. It owns the // elements in the vector. std::vector test_info_list_; -- cgit v1.2.3 From ffeb11d14a890b902dbb26ff2296cda7bf2d31df Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 22 Feb 2011 22:08:59 +0000 Subject: Indents preprocessor directives. --- include/gtest/gtest.h | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 741a3607..03ade855 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -672,6 +672,7 @@ class GTEST_API_ TestInfo { const TestResult* result() const { return &result_; } private: + #if GTEST_HAS_DEATH_TEST friend class internal::DefaultDeathTestFactory; #endif // GTEST_HAS_DEATH_TEST @@ -1305,8 +1306,8 @@ AssertionResult CmpHelperEQ(const char* expected_expression, const T1& expected, const T2& actual) { #ifdef _MSC_VER -#pragma warning(push) // Saves the current warning state. -#pragma warning(disable:4389) // Temporarily disables warning on +# pragma warning(push) // Saves the current warning state. +# pragma warning(disable:4389) // Temporarily disables warning on // signed/unsigned mismatch. #endif @@ -1315,7 +1316,7 @@ AssertionResult CmpHelperEQ(const char* expected_expression, } #ifdef _MSC_VER -#pragma warning(pop) // Restores the warning state. +# pragma warning(pop) // Restores the warning state. #endif return EqFailure(expected_expression, @@ -1740,7 +1741,7 @@ class TestWithParam : public Test, public WithParamInterface { // Define this macro to 1 to omit the definition of FAIL(), which is a // generic name and clashes with some other libraries. #if !GTEST_DONT_DEFINE_FAIL -#define FAIL() GTEST_FAIL() +# define FAIL() GTEST_FAIL() #endif // Generates a success with a generic message. @@ -1749,7 +1750,7 @@ class TestWithParam : public Test, public WithParamInterface { // Define this macro to 1 to omit the definition of SUCCEED(), which // is a generic name and clashes with some other libraries. #if !GTEST_DONT_DEFINE_SUCCEED -#define SUCCEED() GTEST_SUCCEED() +# define SUCCEED() GTEST_SUCCEED() #endif // Macros for testing exceptions. @@ -1874,27 +1875,27 @@ class TestWithParam : public Test, public WithParamInterface { // ASSERT_XY(), which clashes with some users' own code. #if !GTEST_DONT_DEFINE_ASSERT_EQ -#define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) +# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) #endif #if !GTEST_DONT_DEFINE_ASSERT_NE -#define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) +# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) #endif #if !GTEST_DONT_DEFINE_ASSERT_LE -#define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) +# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) #endif #if !GTEST_DONT_DEFINE_ASSERT_LT -#define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) +# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) #endif #if !GTEST_DONT_DEFINE_ASSERT_GE -#define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) +# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) #endif #if !GTEST_DONT_DEFINE_ASSERT_GT -#define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) +# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) #endif // C String Comparisons. All tests treat NULL and any non-NULL string @@ -1993,16 +1994,16 @@ GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, // expected result and the actual result with both a human-readable // string representation of the error, if available, as well as the // hex result code. -#define EXPECT_HRESULT_SUCCEEDED(expr) \ +# define EXPECT_HRESULT_SUCCEEDED(expr) \ EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) -#define ASSERT_HRESULT_SUCCEEDED(expr) \ +# define ASSERT_HRESULT_SUCCEEDED(expr) \ ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) -#define EXPECT_HRESULT_FAILED(expr) \ +# define EXPECT_HRESULT_FAILED(expr) \ EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) -#define ASSERT_HRESULT_FAILED(expr) \ +# define ASSERT_HRESULT_FAILED(expr) \ ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) #endif // GTEST_OS_WINDOWS @@ -2105,7 +2106,7 @@ bool StaticAssertTypeEq() { // Define this macro to 1 to omit the definition of TEST(), which // is a generic name and clashes with some other libraries. #if !GTEST_DONT_DEFINE_TEST -#define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name) +# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name) #endif // Defines a test that uses a test fixture. -- cgit v1.2.3 From 603533a0a4dcfc2ef33051b9ae8237568a19adc4 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Sat, 5 Mar 2011 08:04:01 +0000 Subject: Fixes compatibility with Borland C++Builder. Original patch by Josh Kelley. Simplified by Zhanyong Wan. --- include/gtest/gtest.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 03ade855..cd01c7ba 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1296,7 +1296,9 @@ namespace internal { template String FormatForComparisonFailureMessage(const T1& value, const T2& /* other_operand */) { - return PrintToString(value); + // C++Builder compiles this incorrectly if the namespace isn't explicitly + // given. + return ::testing::PrintToString(value); } // The helper function for {ASSERT|EXPECT}_EQ. -- cgit v1.2.3 From 431a8be1662a3bc9601240914f412b0436d94703 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 5 Oct 2011 05:52:34 +0000 Subject: Implements the timestamp attribute for the testsuites element in the output XML (external contribution by Dirk Meister). --- include/gtest/gtest.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index cd01c7ba..432ee2d6 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1152,6 +1152,10 @@ class GTEST_API_ UnitTest { // Gets the number of tests that should run. int test_to_run_count() const; + // Gets the time of the test program start, in ms from the start of the + // UNIX epoch. + TimeInMillis start_timestamp() const; + // Gets the elapsed time, in milliseconds. TimeInMillis elapsed_time() const; -- cgit v1.2.3 From 4c11f25f8c972bc5bed6d92abe2a0a3e41f499d7 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Mon, 24 Oct 2011 21:13:56 +0000 Subject: Expressed the thread-safety annotations in code, replacing the existing comment-based system (by Aaron Jacobs). --- include/gtest/gtest.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 432ee2d6..c1969906 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1107,11 +1107,13 @@ class GTEST_API_ UnitTest { // Returns the TestCase object for the test that's currently running, // or NULL if no test is running. - const TestCase* current_test_case() const; + const TestCase* current_test_case() const + GTEST_LOCK_EXCLUDED_(mutex_); // Returns the TestInfo object for the test that's currently running, // or NULL if no test is running. - const TestInfo* current_test_info() const; + const TestInfo* current_test_info() const + GTEST_LOCK_EXCLUDED_(mutex_); // Returns the random seed used at the start of the current test run. int random_seed() const; @@ -1121,7 +1123,8 @@ class GTEST_API_ UnitTest { // value-parameterized tests and instantiate and register them. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - internal::ParameterizedTestCaseRegistry& parameterized_test_registry(); + internal::ParameterizedTestCaseRegistry& parameterized_test_registry() + GTEST_LOCK_EXCLUDED_(mutex_); #endif // GTEST_HAS_PARAM_TEST // Gets the number of successful test cases. @@ -1194,7 +1197,8 @@ class GTEST_API_ UnitTest { const char* file_name, int line_number, const internal::String& message, - const internal::String& os_stack_trace); + const internal::String& os_stack_trace) + GTEST_LOCK_EXCLUDED_(mutex_); // Adds a TestProperty to the current TestResult object. If the result already // contains a property with the same key, the value will be updated. @@ -1227,10 +1231,12 @@ class GTEST_API_ UnitTest { // Pushes a trace defined by SCOPED_TRACE() on to the per-thread // Google Test trace stack. - void PushGTestTrace(const internal::TraceInfo& trace); + void PushGTestTrace(const internal::TraceInfo& trace) + GTEST_LOCK_EXCLUDED_(mutex_); // Pops a trace from the per-thread Google Test trace stack. - void PopGTestTrace(); + void PopGTestTrace() + GTEST_LOCK_EXCLUDED_(mutex_); // Protects mutable state in *impl_. This is mutable as some const // methods need to lock it too. -- cgit v1.2.3 From 8965a6a0d2165f32e6413594bba6367f271f51e7 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Fri, 4 Nov 2011 17:56:23 +0000 Subject: Improves conformance to the Google C++ Style Guide (by Greg Miller). --- include/gtest/gtest.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index c1969906..2d570a23 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -52,6 +52,7 @@ #define GTEST_INCLUDE_GTEST_GTEST_H_ #include +#include #include #include "gtest/internal/gtest-internal.h" @@ -672,7 +673,6 @@ class GTEST_API_ TestInfo { const TestResult* result() const { return &result_; } private: - #if GTEST_HAS_DEATH_TEST friend class internal::DefaultDeathTestFactory; #endif // GTEST_HAS_DEATH_TEST @@ -1456,11 +1456,11 @@ GTEST_IMPL_CMP_HELPER_(NE, !=); // Implements the helper function for {ASSERT|EXPECT}_LE GTEST_IMPL_CMP_HELPER_(LE, <=); // Implements the helper function for {ASSERT|EXPECT}_LT -GTEST_IMPL_CMP_HELPER_(LT, < ); +GTEST_IMPL_CMP_HELPER_(LT, <); // Implements the helper function for {ASSERT|EXPECT}_GE GTEST_IMPL_CMP_HELPER_(GE, >=); // Implements the helper function for {ASSERT|EXPECT}_GT -GTEST_IMPL_CMP_HELPER_(GT, > ); +GTEST_IMPL_CMP_HELPER_(GT, >); #undef GTEST_IMPL_CMP_HELPER_ -- cgit v1.2.3 From f0b86fc3b0f625e1db84f3632cb37bd9eae6ae19 Mon Sep 17 00:00:00 2001 From: jgm Date: Fri, 9 Mar 2012 17:12:39 +0000 Subject: Misc small updates to some debug death code, and to messages streaming to macros --- include/gtest/gtest.h | 6 ------ 1 file changed, 6 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 2d570a23..226307ec 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1731,12 +1731,6 @@ class TestWithParam : public Test, public WithParamInterface { // usually want the fail-fast behavior of FAIL and ASSERT_*, but those // writing data-driven tests often find themselves using ADD_FAILURE // and EXPECT_* more. -// -// Examples: -// -// EXPECT_TRUE(server.StatusIsOK()); -// ASSERT_FALSE(server.HasPendingRequest(port)) -// << "There are still pending requests " << "on port " << port; // Generates a nonfatal failure with a generic message. #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") -- cgit v1.2.3 From a88c9a88e49e90ec414175543b2b7ff2f70866a7 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 7 Jun 2012 20:34:34 +0000 Subject: Improves gtest's failure messages. In particulars, char pointers and char arrays are not escapped properly. --- include/gtest/gtest.h | 97 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 10 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 226307ec..a13cfeb8 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1291,24 +1291,101 @@ GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); namespace internal { +// FormatForComparison::Format(value) formats a +// value of type ToPrint that is an operand of a comparison assertion +// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in +// the comparison, and is used to help determine the best way to +// format the value. In particular, when the value is a C string +// (char pointer) and the other operand is an STL string object, we +// want to format the C string as a string, since we know it is +// compared by value with the string object. If the value is a char +// pointer but the other operand is not an STL string object, we don't +// know whether the pointer is supposed to point to a NUL-terminated +// string, and thus want to print it as a pointer to be safe. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + +// The default case. +template +class FormatForComparison { + public: + static ::std::string Format(const ToPrint& value) { + return ::testing::PrintToString(value); + } +}; + +// Array. +template +class FormatForComparison { + public: + static ::std::string Format(const ToPrint* value) { + return FormatForComparison::Format(value); + } +}; + +// By default, print C string as pointers to be safe, as we don't know +// whether they actually point to a NUL-terminated string. + +#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ + template \ + class FormatForComparison { \ + public: \ + static ::std::string Format(CharType* value) { \ + return ::testing::PrintToString(static_cast(value)); \ + } \ + } + +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); + +#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ + +// If a C string is compared with an STL string object, we know it's meant +// to point to a NUL-terminated string, and thus can print it as a string. + +#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ + template <> \ + class FormatForComparison { \ + public: \ + static ::std::string Format(CharType* value) { \ + return ::testing::PrintToString(value); \ + } \ + } + +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); + +#if GTEST_HAS_GLOBAL_STRING +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string); +#endif + +#if GTEST_HAS_GLOBAL_WSTRING +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring); +#endif + +#if GTEST_HAS_STD_WSTRING +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); +#endif + +#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ + // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) // operand to be used in a failure message. The type (but not value) // of the other operand may affect the format. This allows us to // print a char* as a raw pointer when it is compared against another -// char*, and print it as a C string when it is compared against an -// std::string object, for example. -// -// The default implementation ignores the type of the other operand. -// Some specialized versions are used to handle formatting wide or -// narrow C strings. +// char* or void*, and print it as a C string when it is compared +// against an std::string object, for example. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. template String FormatForComparisonFailureMessage(const T1& value, const T2& /* other_operand */) { - // C++Builder compiles this incorrectly if the namespace isn't explicitly - // given. - return ::testing::PrintToString(value); + return FormatForComparison::Format(value); } // The helper function for {ASSERT|EXPECT}_EQ. @@ -1320,7 +1397,7 @@ AssertionResult CmpHelperEQ(const char* expected_expression, #ifdef _MSC_VER # pragma warning(push) // Saves the current warning state. # pragma warning(disable:4389) // Temporarily disables warning on - // signed/unsigned mismatch. + // signed/unsigned mismatch. #endif if (expected == actual) { -- cgit v1.2.3 From 87fdda2cf24d953f3cbec1e0c266b2db9928f406 Mon Sep 17 00:00:00 2001 From: jgm Date: Thu, 15 Nov 2012 15:47:38 +0000 Subject: Unfortunately, the svn repo is a bit out of date. This commit contains 8 changes that haven't made it to svn. The descriptions of each change are listed below. - Fixes some python shebang lines. - Add ElementsAreArray overloads to gmock. ElementsAreArray now makes a copy of its input elements before the conversion to a Matcher. ElementsAreArray can now take a vector as input. ElementsAreArray can now take an iterator pair as input. - Templatize MatchAndExplain to allow independent string types for the matcher and matchee. I also templatized the ConstCharPointer version of MatchAndExplain to avoid calls with "char*" from using the new templated MatchAndExplain. - Fixes the bug where the constructor of the return type of ElementsAre() saves a reference instead of a copy of the arguments. - Extends ElementsAre() to accept arrays whose sizes aren't known. - Switches gTest's internal FilePath class from testing::internal::String to std::string. testing::internal::String was introduced when gTest couldn't depend on std::string. It's now deprecated. - Switches gTest & gMock from using testing::internal::String objects to std::string. Some static methods of String are still in use. We may be able to remove some but not all of them. In particular, String::Format() should eventually be removed as it truncates the result at 4096 characters, often causing problems. --- include/gtest/gtest.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index a13cfeb8..9ecb1456 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -160,9 +160,9 @@ class TestEventRepeater; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResult::Type result_type, - const String& message); + const std::string& message); -// Converts a streamable value to a String. A NULL pointer is +// Converts a streamable value to an std::string. A NULL pointer is // converted to "(null)". When the input value is a ::string, // ::std::string, ::wstring, or ::std::wstring object, each NUL // character in it is replaced with "\\0". @@ -170,7 +170,7 @@ void ReportFailureInUnknownLocation(TestPartResult::Type result_type, // to the definition of the Message class, required by the ARM // compiler. template -String StreamableToString(const T& streamable) { +std::string StreamableToString(const T& streamable) { return (Message() << streamable).GetString(); } @@ -495,9 +495,9 @@ class TestProperty { private: // The key supplied by the user. - internal::String key_; + std::string key_; // The value supplied by the user. - internal::String value_; + std::string value_; }; // The result of a single Test. This includes a list of @@ -869,7 +869,7 @@ class GTEST_API_ TestCase { void UnshuffleTests(); // Name of the test case. - internal::String name_; + std::string name_; // Name of the parameter type, or NULL if this is not a typed or a // type-parameterized test. const internal::scoped_ptr type_param_; @@ -1196,8 +1196,8 @@ class GTEST_API_ UnitTest { void AddTestPartResult(TestPartResult::Type result_type, const char* file_name, int line_number, - const internal::String& message, - const internal::String& os_stack_trace) + const std::string& message, + const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_); // Adds a TestProperty to the current TestResult object. If the result already @@ -1221,7 +1221,7 @@ class GTEST_API_ UnitTest { friend internal::UnitTestImpl* internal::GetUnitTestImpl(); friend void internal::ReportFailureInUnknownLocation( TestPartResult::Type result_type, - const internal::String& message); + const std::string& message); // Creates an empty UnitTest. UnitTest(); @@ -1383,8 +1383,8 @@ GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. template -String FormatForComparisonFailureMessage(const T1& value, - const T2& /* other_operand */) { +std::string FormatForComparisonFailureMessage( + const T1& value, const T2& /* other_operand */) { return FormatForComparison::Format(value); } @@ -1701,9 +1701,9 @@ class GTEST_API_ AssertHelper { : type(t), file(srcfile), line(line_num), message(msg) { } TestPartResult::Type const type; - const char* const file; - int const line; - String const message; + const char* const file; + int const line; + std::string const message; private: GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); @@ -1981,7 +1981,7 @@ class TestWithParam : public Test, public WithParamInterface { # define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) #endif -// C String Comparisons. All tests treat NULL and any non-NULL string +// C-string Comparisons. All tests treat NULL and any non-NULL string // as different. Two NULLs are equal. // // * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 -- cgit v1.2.3 From cc1fdb58caf8d5ac9b858f615d3c42267fc5e258 Mon Sep 17 00:00:00 2001 From: kosak Date: Fri, 22 Feb 2013 20:10:40 +0000 Subject: Removes testing::internal::String::Format(), which causes problems as it truncates the result at 4096 chars. Also update an obsolete link in comment. --- include/gtest/gtest.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 9ecb1456..09e74d91 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -680,7 +680,8 @@ class GTEST_API_ TestInfo { friend class TestCase; friend class internal::UnitTestImpl; friend TestInfo* internal::MakeAndRegisterTestInfo( - const char* test_case_name, const char* name, + const char* test_case_name, + const char* name, const char* type_param, const char* value_param, internal::TypeId fixture_class_id, @@ -690,9 +691,10 @@ class GTEST_API_ TestInfo { // Constructs a TestInfo object. The newly constructed instance assumes // ownership of the factory object. - TestInfo(const char* test_case_name, const char* name, - const char* a_type_param, - const char* a_value_param, + TestInfo(const std::string& test_case_name, + const std::string& name, + const char* a_type_param, // NULL if not a type-parameterized test + const char* a_value_param, // NULL if not a value-parameterized test internal::TypeId fixture_class_id, internal::TestFactoryBase* factory); -- cgit v1.2.3 From ba072ccca41212e3ac3ac1eca3381d226187c0d1 Mon Sep 17 00:00:00 2001 From: kosak Date: Fri, 22 Feb 2013 20:25:42 +0000 Subject: Fixes gUnit streaming output format. --- include/gtest/gtest.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 09e74d91..e53cd9f5 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -154,6 +154,7 @@ class ExecDeathTest; class NoExecDeathTest; class FinalSuccessChecker; class GTestFlagSaver; +class StreamingListenerTest; class TestResultAccessor; class TestEventListenersAccessor; class TestEventRepeater; @@ -679,6 +680,7 @@ class GTEST_API_ TestInfo { friend class Test; friend class TestCase; friend class internal::UnitTestImpl; + friend class internal::StreamingListenerTest; friend TestInfo* internal::MakeAndRegisterTestInfo( const char* test_case_name, const char* name, @@ -1219,6 +1221,7 @@ class GTEST_API_ UnitTest { friend class Test; friend class internal::AssertHelper; friend class internal::ScopedTrace; + friend class internal::StreamingListenerTest; friend Environment* AddGlobalTestEnvironment(Environment* env); friend internal::UnitTestImpl* internal::GetUnitTestImpl(); friend void internal::ReportFailureInUnknownLocation( -- cgit v1.2.3 From b3ed14ac17f8f1d218a6f8dfaa18d45621cd33ff Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 28 Feb 2013 23:29:06 +0000 Subject: Implements RUN_ALL_TESTS() as a function. --- include/gtest/gtest.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index e53cd9f5..e06082c3 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -2227,15 +2227,20 @@ bool StaticAssertTypeEq() { GTEST_TEST_(test_fixture, test_name, test_fixture, \ ::testing::internal::GetTypeId()) -// Use this macro in main() to run all tests. It returns 0 if all +} // namespace testing + +// Use this function in main() to run all tests. It returns 0 if all // tests are successful, or 1 otherwise. // // RUN_ALL_TESTS() should be invoked after the command line has been // parsed by InitGoogleTest(). +// +// This function was formerly a macro; thus, it is in the global +// namespace and has an all-caps name. +int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; -#define RUN_ALL_TESTS()\ - (::testing::UnitTest::GetInstance()->Run()) - -} // namespace testing +inline int RUN_ALL_TESTS() { + return ::testing::UnitTest::GetInstance()->Run(); +} #endif // GTEST_INCLUDE_GTEST_GTEST_H_ -- cgit v1.2.3 From 6a036a5c8c7613e888fad39d92fc3fd84a96fbc7 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 28 Feb 2013 23:46:07 +0000 Subject: Fixes a nasty issue in gtest's template instantiation. --- include/gtest/gtest.h | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index e06082c3..6d13ff65 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -163,18 +163,6 @@ class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResult::Type result_type, const std::string& message); -// Converts a streamable value to an std::string. A NULL pointer is -// converted to "(null)". When the input value is a ::string, -// ::std::string, ::wstring, or ::std::wstring object, each NUL -// character in it is replaced with "\\0". -// Declared in gtest-internal.h but defined here, so that it has access -// to the definition of the Message class, required by the ARM -// compiler. -template -std::string StreamableToString(const T& streamable) { - return (Message() << streamable).GetString(); -} - } // namespace internal // The friend relationship of some of these classes is cyclic. -- cgit v1.2.3 From 1edbcbad73e0c711d5aa0f165bad5e9518894375 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 12 Mar 2013 21:17:22 +0000 Subject: Prints a useful message when GetParam() is called in a non-parameterized test. --- include/gtest/gtest.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 6d13ff65..e2f9a991 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1755,7 +1755,12 @@ class WithParamInterface { // references static data, to reduce the opportunity for incorrect uses // like writing 'WithParamInterface::GetParam()' for a test that // uses a fixture whose parameter type is int. - const ParamType& GetParam() const { return *parameter_; } + const ParamType& GetParam() const { + GTEST_CHECK_(parameter_ != NULL) + << "GetParam() can only be called inside a value-parameterized test " + << "-- did you intend to write TEST_P instead of TEST_F?"; + return *parameter_; + } private: // Sets parameter value. The caller is responsible for making sure the value -- cgit v1.2.3 From f5fa71f728ce77c5d5c1a940a9bd8bb1d64c9f78 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Fri, 5 Apr 2013 20:50:46 +0000 Subject: Implements support for calling Test::RecordProperty() outside of a test. --- include/gtest/gtest.h | 65 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 22 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index e2f9a991..751aa2e8 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -158,6 +158,7 @@ class StreamingListenerTest; class TestResultAccessor; class TestEventListenersAccessor; class TestEventRepeater; +class UnitTestRecordPropertyTestHelper; class WindowsDeathTest; class UnitTestImpl* GetUnitTestImpl(); void ReportFailureInUnknownLocation(TestPartResult::Type result_type, @@ -381,20 +382,21 @@ class GTEST_API_ Test { // non-fatal) failure. static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } - // Logs a property for the current test. Only the last value for a given - // key is remembered. - // These are public static so they can be called from utility functions - // that are not members of the test fixture. - // The arguments are const char* instead strings, as Google Test is used - // on platforms where string doesn't compile. - // - // Note that a driving consideration for these RecordProperty methods - // was to produce xml output suited to the Greenspan charting utility, - // which at present will only chart values that fit in a 32-bit int. It - // is the user's responsibility to restrict their values to 32-bit ints - // if they intend them to be used with Greenspan. - static void RecordProperty(const char* key, const char* value); - static void RecordProperty(const char* key, int value); + // Logs a property for the current test, test case, or for the entire + // invocation of the test program when used outside of the context of a + // test case. Only the last value for a given key is remembered. These + // are public static so they can be called from utility functions that are + // not members of the test fixture. Calls to RecordProperty made during + // lifespan of the test (from the moment its constructor starts to the + // moment its destructor finishes) will be output in XML as attributes of + // the element. Properties recorded from fixture's + // SetUpTestCase or TearDownTestCase are logged as attributes of the + // corresponding element. Calls to RecordProperty made in the + // global context (before or after invocation of RUN_ALL_TESTS and from + // SetUp/TearDown method of Environment objects registered with Google + // Test) will be output as attributes of the element. + static void RecordProperty(const std::string& key, const std::string& value); + static void RecordProperty(const std::string& key, int value); protected: // Creates a Test object. @@ -463,7 +465,7 @@ class TestProperty { // C'tor. TestProperty does NOT have a default constructor. // Always use this constructor (with parameters) to create a // TestProperty object. - TestProperty(const char* a_key, const char* a_value) : + TestProperty(const std::string& a_key, const std::string& a_value) : key_(a_key), value_(a_value) { } @@ -478,7 +480,7 @@ class TestProperty { } // Sets a new value, overriding the one supplied in the constructor. - void SetValue(const char* new_value) { + void SetValue(const std::string& new_value) { value_ = new_value; } @@ -537,6 +539,7 @@ class GTEST_API_ TestResult { private: friend class TestInfo; + friend class TestCase; friend class UnitTest; friend class internal::DefaultGlobalTestPartResultReporter; friend class internal::ExecDeathTest; @@ -561,13 +564,16 @@ class GTEST_API_ TestResult { // a non-fatal failure if invalid (e.g., if it conflicts with reserved // key names). If a property is already recorded for the same key, the // value will be updated, rather than storing multiple values for the same - // key. - void RecordProperty(const TestProperty& test_property); + // key. xml_element specifies the element for which the property is being + // recorded and is used for validation. + void RecordProperty(const std::string& xml_element, + const TestProperty& test_property); // Adds a failure if the key is a reserved attribute of Google Test // testcase tags. Returns true if the property is valid. // TODO(russr): Validate attribute names are legal and human readable. - static bool ValidateTestProperty(const TestProperty& test_property); + static bool ValidateTestProperty(const std::string& xml_element, + const TestProperty& test_property); // Adds a test part result to the list. void AddTestPartResult(const TestPartResult& test_part_result); @@ -792,6 +798,10 @@ class GTEST_API_ TestCase { // total_test_count() - 1. If i is not in that range, returns NULL. const TestInfo* GetTestInfo(int i) const; + // Returns the TestResult that holds test properties recorded during + // execution of SetUpTestCase and TearDownTestCase. + const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } + private: friend class Test; friend class internal::UnitTestImpl; @@ -880,6 +890,9 @@ class GTEST_API_ TestCase { bool should_run_; // Elapsed time, in milliseconds. TimeInMillis elapsed_time_; + // Holds test properties recorded during execution of SetUpTestCase and + // TearDownTestCase. + TestResult ad_hoc_test_result_; // We disallow copying TestCases. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); @@ -1165,6 +1178,10 @@ class GTEST_API_ UnitTest { // total_test_case_count() - 1. If i is not in that range, returns NULL. const TestCase* GetTestCase(int i) const; + // Returns the TestResult containing information on test failures and + // properties logged outside of individual test cases. + const TestResult& ad_hoc_test_result() const; + // Returns the list of event listeners that can be used to track events // inside Google Test. TestEventListeners& listeners(); @@ -1192,9 +1209,12 @@ class GTEST_API_ UnitTest { const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_); - // Adds a TestProperty to the current TestResult object. If the result already - // contains a property with the same key, the value will be updated. - void RecordPropertyForCurrentTest(const char* key, const char* value); + // Adds a TestProperty to the current TestResult object when invoked from + // inside a test, to current TestCase's ad_hoc_test_result_ when invoked + // from SetUpTestCase or TearDownTestCase, or to the global property set + // when invoked elsewhere. If the result already contains a property with + // the same key, the value will be updated. + void RecordProperty(const std::string& key, const std::string& value); // Gets the i-th test case among all the test cases. i can range from 0 to // total_test_case_count() - 1. If i is not in that range, returns NULL. @@ -1210,6 +1230,7 @@ class GTEST_API_ UnitTest { friend class internal::AssertHelper; friend class internal::ScopedTrace; friend class internal::StreamingListenerTest; + friend class internal::UnitTestRecordPropertyTestHelper; friend Environment* AddGlobalTestEnvironment(Environment* env); friend internal::UnitTestImpl* internal::GetUnitTestImpl(); friend void internal::ReportFailureInUnknownLocation( -- cgit v1.2.3 From c506784b08473f3ba8f37d588fd08d8d3f948245 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 25 Apr 2013 17:58:52 +0000 Subject: When --gtest_filter is specified, XML report now doesn't contain information about tests that are filtered out (issue 141). --- include/gtest/gtest.h | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 751aa2e8..6fa0a392 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -646,9 +646,9 @@ class GTEST_API_ TestInfo { return NULL; } - // Returns true if this test should run, that is if the test is not disabled - // (or it is disabled but the also_run_disabled_tests flag has been specified) - // and its full name matches the user-specified filter. + // Returns true if this test should run, that is if the test is not + // disabled (or it is disabled but the also_run_disabled_tests flag has + // been specified) and its full name matches the user-specified filter. // // Google Test allows the user to filter the tests by their full names. // The full name of a test Bar in test case Foo is defined as @@ -664,6 +664,14 @@ class GTEST_API_ TestInfo { // contains the character 'A' or starts with "Foo.". bool should_run() const { return should_run_; } + // Returns true iff this test will appear in the XML report. + bool is_reportable() const { + // For now, the XML report includes all tests matching the filter. + // In the future, we may trim tests that are excluded because of + // sharding. + return matches_filter_; + } + // Returns the result of the test. const TestResult* result() const { return &result_; } @@ -776,9 +784,15 @@ class GTEST_API_ TestCase { // Gets the number of failed tests in this test case. int failed_test_count() const; + // Gets the number of disabled tests that will be reported in the XML report. + int reportable_disabled_test_count() const; + // Gets the number of disabled tests in this test case. int disabled_test_count() const; + // Gets the number of tests to be printed in the XML report. + int reportable_test_count() const; + // Get the number of tests in this test case that should run. int test_to_run_count() const; @@ -854,11 +868,22 @@ class GTEST_API_ TestCase { return test_info->should_run() && test_info->result()->Failed(); } + // Returns true iff the test is disabled and will be reported in the XML + // report. + static bool TestReportableDisabled(const TestInfo* test_info) { + return test_info->is_reportable() && test_info->is_disabled_; + } + // Returns true iff test is disabled. static bool TestDisabled(const TestInfo* test_info) { return test_info->is_disabled_; } + // Returns true iff this test will appear in the XML report. + static bool TestReportable(const TestInfo* test_info) { + return test_info->is_reportable(); + } + // Returns true if the given test should run. static bool ShouldRunTest(const TestInfo* test_info) { return test_info->should_run(); @@ -1151,9 +1176,15 @@ class GTEST_API_ UnitTest { // Gets the number of failed tests. int failed_test_count() const; + // Gets the number of disabled tests that will be reported in the XML report. + int reportable_disabled_test_count() const; + // Gets the number of disabled tests. int disabled_test_count() const; + // Gets the number of tests to be printed in the XML report. + int reportable_test_count() const; + // Gets the number of all tests. int total_test_count() const; -- cgit v1.2.3 From 8120f66c3249e253f03fdb48bee7e528bc038d31 Mon Sep 17 00:00:00 2001 From: billydonahue Date: Thu, 15 May 2014 19:42:15 +0000 Subject: Push upstream to SVN. --- include/gtest/gtest.h | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 6fa0a392..a19b3db3 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -258,8 +258,31 @@ class GTEST_API_ AssertionResult { // Copy constructor. // Used in EXPECT_TRUE/FALSE(assertion_result). AssertionResult(const AssertionResult& other); + + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) + // Used in the EXPECT_TRUE/FALSE(bool_expression). - explicit AssertionResult(bool success) : success_(success) {} + // + // T must be contextually convertible to bool. + // + // The second parameter prevents this overload from being considered if + // the argument is implicitly convertible to AssertionResult. In that case + // we want AssertionResult's copy constructor to be used. + template + explicit AssertionResult( + const T& success, + typename internal::EnableIf< + !internal::ImplicitlyConvertible::value>::type* + /*enabler*/ = NULL) + : success_(success) {} + + GTEST_DISABLE_MSC_WARNINGS_POP_() + + // Assignment operator. + AssertionResult& operator=(AssertionResult other) { + swap(other); + return *this; + } // Returns true iff the assertion succeeded. operator bool() const { return success_; } // NOLINT @@ -300,6 +323,9 @@ class GTEST_API_ AssertionResult { message_->append(a_message.GetString().c_str()); } + // Swap the contents of this AssertionResult with other. + void swap(AssertionResult& other); + // Stores result of the assertion predicate. bool success_; // Stores the message describing the condition in case the expectation @@ -307,8 +333,6 @@ class GTEST_API_ AssertionResult { // Referenced via a pointer to avoid taking too much stack frame space // with test assertions. internal::scoped_ptr< ::std::string> message_; - - GTEST_DISALLOW_ASSIGN_(AssertionResult); }; // Makes a successful assertion result. @@ -1439,19 +1463,11 @@ AssertionResult CmpHelperEQ(const char* expected_expression, const char* actual_expression, const T1& expected, const T2& actual) { -#ifdef _MSC_VER -# pragma warning(push) // Saves the current warning state. -# pragma warning(disable:4389) // Temporarily disables warning on - // signed/unsigned mismatch. -#endif - +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4389 /* signed/unsigned mismatch */) if (expected == actual) { return AssertionSuccess(); } - -#ifdef _MSC_VER -# pragma warning(pop) // Restores the warning state. -#endif +GTEST_DISABLE_MSC_WARNINGS_POP_() return EqFailure(expected_expression, actual_expression, -- cgit v1.2.3 From 96ddffe8fdabccb10fb693a54dcb88bd5b71bc09 Mon Sep 17 00:00:00 2001 From: kosak Date: Wed, 18 Jun 2014 00:22:42 +0000 Subject: Reduce the number of occurrences of gendered pronouns in gtest. --- include/gtest/gtest.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index a19b3db3..38ca3e97 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -70,14 +70,14 @@ // class ::string, which has the same interface as ::std::string, but // has a different implementation. // -// The user can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that +// You can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that // ::string is available AND is a distinct type to ::std::string, or // define it to 0 to indicate otherwise. // -// If the user's ::std::string and ::string are the same class due to -// aliasing, he should define GTEST_HAS_GLOBAL_STRING to 0. +// If ::std::string and ::string are the same class on your platform +// due to aliasing, you should define GTEST_HAS_GLOBAL_STRING to 0. // -// If the user doesn't define GTEST_HAS_GLOBAL_STRING, it is defined +// If you do not define GTEST_HAS_GLOBAL_STRING, it is defined // heuristically. namespace testing { @@ -455,17 +455,17 @@ class GTEST_API_ Test { // Uses a GTestFlagSaver to save and restore all Google Test flags. const internal::GTestFlagSaver* const gtest_flag_saver_; - // Often a user mis-spells SetUp() as Setup() and spends a long time + // Often a user misspells SetUp() as Setup() and spends a long time // wondering why it is never called by Google Test. The declaration of // the following method is solely for catching such an error at // compile time: // // - The return type is deliberately chosen to be not void, so it - // will be a conflict if a user declares void Setup() in his test - // fixture. + // will be a conflict if void Setup() is declared in the user's + // test fixture. // // - This method is private, so it will be another compiler error - // if a user calls it from his test fixture. + // if the method is called from the user's test fixture. // // DO NOT OVERRIDE THIS FUNCTION. // @@ -948,7 +948,7 @@ class GTEST_API_ TestCase { }; // An Environment object is capable of setting up and tearing down an -// environment. The user should subclass this to define his own +// environment. You should subclass this to define your own // environment(s). // // An Environment object does the set-up and tear-down in virtual @@ -2231,8 +2231,8 @@ bool StaticAssertTypeEq() { // The convention is to end the test case name with "Test". For // example, a test case for the Foo class can be named FooTest. // -// The user should put his test code between braces after using this -// macro. Example: +// Test code should appear between braces after an invocation of +// this macro. Example: // // TEST(FooTest, InitializesCorrectly) { // Foo foo; -- cgit v1.2.3 From 6884259b7d57bc8d70771f19c7d492e89d2bfa9a Mon Sep 17 00:00:00 2001 From: kosak Date: Mon, 17 Nov 2014 00:06:22 +0000 Subject: Reduce the stack frame size for CmpHelper* functions by moving the failure path into their own functions. --- include/gtest/gtest.h | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 38ca3e97..d7e3e264 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1457,6 +1457,20 @@ std::string FormatForComparisonFailureMessage( return FormatForComparison::Format(value); } +// Separate the error generating code from the code path to reduce the stack +// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers +// when calling EXPECT_* in a tight loop. +template +AssertionResult CmpHelperEQFailure(const char* expected_expression, + const char* actual_expression, + const T1& expected, const T2& actual) { + return EqFailure(expected_expression, + actual_expression, + FormatForComparisonFailureMessage(expected, actual), + FormatForComparisonFailureMessage(actual, expected), + false); +} + // The helper function for {ASSERT|EXPECT}_EQ. template AssertionResult CmpHelperEQ(const char* expected_expression, @@ -1469,11 +1483,8 @@ GTEST_DISABLE_MSC_WARNINGS_PUSH_(4389 /* signed/unsigned mismatch */) } GTEST_DISABLE_MSC_WARNINGS_POP_() - return EqFailure(expected_expression, - actual_expression, - FormatForComparisonFailureMessage(expected, actual), - FormatForComparisonFailureMessage(actual, expected), - false); + return CmpHelperEQFailure(expected_expression, actual_expression, expected, + actual); } // With this overloaded version, we allow anonymous enums to be used @@ -1561,6 +1572,19 @@ class EqHelper { } }; +// Separate the error generating code from the code path to reduce the stack +// frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers +// when calling EXPECT_OP in a tight loop. +template +AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, + const T1& val1, const T2& val2, + const char* op) { + return AssertionFailure() + << "Expected: (" << expr1 << ") " << op << " (" << expr2 + << "), actual: " << FormatForComparisonFailureMessage(val1, val2) + << " vs " << FormatForComparisonFailureMessage(val2, val1); +} + // A macro for implementing the helper functions needed to implement // ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste // of similar code. @@ -1571,6 +1595,7 @@ class EqHelper { // with gcc 4. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + #define GTEST_IMPL_CMP_HELPER_(op_name, op)\ template \ AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ @@ -1578,10 +1603,7 @@ AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ if (val1 op val2) {\ return AssertionSuccess();\ } else {\ - return AssertionFailure() \ - << "Expected: (" << expr1 << ") " #op " (" << expr2\ - << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ - << " vs " << FormatForComparisonFailureMessage(val2, val1);\ + return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\ }\ }\ GTEST_API_ AssertionResult CmpHelper##op_name(\ -- cgit v1.2.3 From c2101c28771d8abd0f2e057cdbdc26b7a05fad2d Mon Sep 17 00:00:00 2001 From: kosak Date: Thu, 8 Jan 2015 02:35:11 +0000 Subject: Change an example to use 'override' rather than 'virtual'. Add missing headers for 'connect' and 'socket'. --- include/gtest/gtest.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index d7e3e264..71d552e1 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -359,8 +359,8 @@ GTEST_API_ AssertionResult AssertionFailure(const Message& msg); // // class FooTest : public testing::Test { // protected: -// virtual void SetUp() { ... } -// virtual void TearDown() { ... } +// void SetUp() override { ... } +// void TearDown() override { ... } // ... // }; // -- cgit v1.2.3 From 0928adbfea9b7645e884bd95fee23cfd669729cd Mon Sep 17 00:00:00 2001 From: kosak Date: Tue, 14 Jul 2015 22:44:39 +0000 Subject: Move the selection of the flag saver implementation into gtest-port.h and custom/gtest-port.h. --- include/gtest/gtest.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 71d552e1..919df651 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -452,8 +452,7 @@ class GTEST_API_ Test { // internal method to avoid clashing with names used in user TESTs. void DeleteSelf_() { delete this; } - // Uses a GTestFlagSaver to save and restore all Google Test flags. - const internal::GTestFlagSaver* const gtest_flag_saver_; + const internal::scoped_ptr< GTEST_FLAG_SAVER_ > gtest_flag_saver_; // Often a user misspells SetUp() as Setup() and spends a long time // wondering why it is never called by Google Test. The declaration of -- cgit v1.2.3 From 4f8dc917ebce062f75defee3d4890bbcd07e277b Mon Sep 17 00:00:00 2001 From: kosak Date: Fri, 17 Jul 2015 22:11:58 +0000 Subject: Add support for --gtest_flagfile. --- include/gtest/gtest.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 919df651..3f080b84 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -669,6 +669,12 @@ class GTEST_API_ TestInfo { return NULL; } + // Returns the file name where this test is defined. + const char* file() const { return location_.file.c_str(); } + + // Returns the line where this test is defined. + int line() const { return location_.line; } + // Returns true if this test should run, that is if the test is not // disabled (or it is disabled but the also_run_disabled_tests flag has // been specified) and its full name matches the user-specified filter. @@ -711,6 +717,7 @@ class GTEST_API_ TestInfo { const char* name, const char* type_param, const char* value_param, + internal::CodeLocation code_location, internal::TypeId fixture_class_id, Test::SetUpTestCaseFunc set_up_tc, Test::TearDownTestCaseFunc tear_down_tc, @@ -722,6 +729,7 @@ class GTEST_API_ TestInfo { const std::string& name, const char* a_type_param, // NULL if not a type-parameterized test const char* a_value_param, // NULL if not a value-parameterized test + internal::CodeLocation a_code_location, internal::TypeId fixture_class_id, internal::TestFactoryBase* factory); @@ -748,6 +756,7 @@ class GTEST_API_ TestInfo { // Text representation of the value parameter, or NULL if this is not a // value-parameterized test. const internal::scoped_ptr value_param_; + internal::CodeLocation location_; const internal::TypeId fixture_class_id_; // ID of the test fixture class bool should_run_; // True iff this test should run bool is_disabled_; // True iff this test is disabled -- cgit v1.2.3 From 6f8a66431cb592dad629028a50b3dd418a408c87 Mon Sep 17 00:00:00 2001 From: kosak Date: Tue, 28 Jul 2015 00:39:46 +0000 Subject: Introduce FormatForComparison to format values that are operands of comparison assertions (e.g. ASSERT_EQ). --- include/gtest/gtest.h | 97 --------------------------------------------------- 1 file changed, 97 deletions(-) (limited to 'include/gtest/gtest.h') diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 3f080b84..7b59c492 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1368,103 +1368,6 @@ GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); namespace internal { -// FormatForComparison::Format(value) formats a -// value of type ToPrint that is an operand of a comparison assertion -// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in -// the comparison, and is used to help determine the best way to -// format the value. In particular, when the value is a C string -// (char pointer) and the other operand is an STL string object, we -// want to format the C string as a string, since we know it is -// compared by value with the string object. If the value is a char -// pointer but the other operand is not an STL string object, we don't -// know whether the pointer is supposed to point to a NUL-terminated -// string, and thus want to print it as a pointer to be safe. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - -// The default case. -template -class FormatForComparison { - public: - static ::std::string Format(const ToPrint& value) { - return ::testing::PrintToString(value); - } -}; - -// Array. -template -class FormatForComparison { - public: - static ::std::string Format(const ToPrint* value) { - return FormatForComparison::Format(value); - } -}; - -// By default, print C string as pointers to be safe, as we don't know -// whether they actually point to a NUL-terminated string. - -#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ - template \ - class FormatForComparison { \ - public: \ - static ::std::string Format(CharType* value) { \ - return ::testing::PrintToString(static_cast(value)); \ - } \ - } - -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); - -#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ - -// If a C string is compared with an STL string object, we know it's meant -// to point to a NUL-terminated string, and thus can print it as a string. - -#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ - template <> \ - class FormatForComparison { \ - public: \ - static ::std::string Format(CharType* value) { \ - return ::testing::PrintToString(value); \ - } \ - } - -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); - -#if GTEST_HAS_GLOBAL_STRING -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string); -#endif - -#if GTEST_HAS_GLOBAL_WSTRING -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring); -#endif - -#if GTEST_HAS_STD_WSTRING -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); -#endif - -#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ - -// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) -// operand to be used in a failure message. The type (but not value) -// of the other operand may affect the format. This allows us to -// print a char* as a raw pointer when it is compared against another -// char* or void*, and print it as a C string when it is compared -// against an std::string object, for example. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -template -std::string FormatForComparisonFailureMessage( - const T1& value, const T2& /* other_operand */) { - return FormatForComparison::Format(value); -} - // Separate the error generating code from the code path to reduce the stack // frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers // when calling EXPECT_* in a tight loop. -- cgit v1.2.3