Commit aaebfcdc by zhanyong.wan

Refactors for the event listener API (by Vlad Losev): hides some methods in…

Refactors for the event listener API (by Vlad Losev): hides some methods in UnitTest; implements the result printers using the public API.
parent e6095dee
...@@ -141,8 +141,12 @@ const int kMaxStackTraceDepth = 100; ...@@ -141,8 +141,12 @@ const int kMaxStackTraceDepth = 100;
namespace internal { namespace internal {
class AssertHelper;
class GTestFlagSaver; class GTestFlagSaver;
class TestCase; // A collection of related tests. 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 // Converts a streamable value to a String. A NULL pointer is
// converted to "(null)". When the input value is a ::string, // converted to "(null)". When the input value is a ::string,
...@@ -759,33 +763,6 @@ class UnitTest { ...@@ -759,33 +763,6 @@ class UnitTest {
// Consecutive calls will return the same object. // Consecutive calls will return the same object.
static UnitTest* GetInstance(); 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. // Runs all tests in this UnitTest object and prints the result.
// Returns 0 if successful, or 1 otherwise. // Returns 0 if successful, or 1 otherwise.
// //
...@@ -809,14 +786,41 @@ class UnitTest { ...@@ -809,14 +786,41 @@ class UnitTest {
#if GTEST_HAS_PARAM_TEST #if GTEST_HAS_PARAM_TEST
// Returns the ParameterizedTestCaseRegistry object used to keep track of // Returns the ParameterizedTestCaseRegistry object used to keep track of
// value-parameterized tests and instantiate and register them. // 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();
#endif // GTEST_HAS_PARAM_TEST #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. // Accessors for the implementation object.
internal::UnitTestImpl* impl() { return impl_; } internal::UnitTestImpl* impl() { return impl_; }
const internal::UnitTestImpl* impl() const { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; }
private:
// Gets the number of successful test cases. // Gets the number of successful test cases.
int successful_test_case_count() const; int successful_test_case_count() const;
...@@ -861,7 +865,20 @@ class UnitTest { ...@@ -861,7 +865,20 @@ class UnitTest {
// ScopedTrace is a friend as it needs to modify the per-thread // ScopedTrace is a friend as it needs to modify the per-thread
// trace stack, which is a private member of UnitTest. // 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 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. // Creates an empty UnitTest.
UnitTest(); UnitTest();
......
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
namespace testing { namespace testing {
using internal::GetUnitTestImpl;
// Gets the summary of the failure message by omitting the stack trace // Gets the summary of the failure message by omitting the stack trace
// in it. // in it.
internal::String TestPartResult::ExtractSummary(const char* message) { internal::String TestPartResult::ExtractSummary(const char* message) {
...@@ -101,14 +103,13 @@ namespace internal { ...@@ -101,14 +103,13 @@ namespace internal {
HasNewFatalFailureHelper::HasNewFatalFailureHelper() HasNewFatalFailureHelper::HasNewFatalFailureHelper()
: has_new_fatal_failure_(false), : has_new_fatal_failure_(false),
original_reporter_(UnitTest::GetInstance()->impl()-> original_reporter_(GetUnitTestImpl()->
GetTestPartResultReporterForCurrentThread()) { GetTestPartResultReporterForCurrentThread()) {
UnitTest::GetInstance()->impl()->SetTestPartResultReporterForCurrentThread( GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
this);
} }
HasNewFatalFailureHelper::~HasNewFatalFailureHelper() { HasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
UnitTest::GetInstance()->impl()->SetTestPartResultReporterForCurrentThread( GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
original_reporter_); original_reporter_);
} }
......
...@@ -67,6 +67,7 @@ using testing::internal::DeathTest; ...@@ -67,6 +67,7 @@ using testing::internal::DeathTest;
using testing::internal::DeathTestFactory; using testing::internal::DeathTestFactory;
using testing::internal::FilePath; using testing::internal::FilePath;
using testing::internal::GetLastErrnoDescription; using testing::internal::GetLastErrnoDescription;
using testing::internal::GetUnitTestImpl;
using testing::internal::ParseNaturalNumber; using testing::internal::ParseNaturalNumber;
using testing::internal::String; using testing::internal::String;
...@@ -77,22 +78,22 @@ namespace internal { ...@@ -77,22 +78,22 @@ namespace internal {
// single UnitTest object during their lifetimes. // single UnitTest object during their lifetimes.
class ReplaceDeathTestFactory { class ReplaceDeathTestFactory {
public: public:
ReplaceDeathTestFactory(UnitTest* parent, DeathTestFactory* new_factory) explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory)
: parent_impl_(parent->impl()) { : unit_test_impl_(GetUnitTestImpl()) {
old_factory_ = parent_impl_->death_test_factory_.release(); old_factory_ = unit_test_impl_->death_test_factory_.release();
parent_impl_->death_test_factory_.reset(new_factory); unit_test_impl_->death_test_factory_.reset(new_factory);
} }
~ReplaceDeathTestFactory() { ~ReplaceDeathTestFactory() {
parent_impl_->death_test_factory_.release(); unit_test_impl_->death_test_factory_.release();
parent_impl_->death_test_factory_.reset(old_factory_); unit_test_impl_->death_test_factory_.reset(old_factory_);
} }
private: private:
// Prevents copying ReplaceDeathTestFactory objects. // Prevents copying ReplaceDeathTestFactory objects.
ReplaceDeathTestFactory(const ReplaceDeathTestFactory&); ReplaceDeathTestFactory(const ReplaceDeathTestFactory&);
void operator=(const ReplaceDeathTestFactory&); void operator=(const ReplaceDeathTestFactory&);
UnitTestImpl* parent_impl_; UnitTestImpl* unit_test_impl_;
DeathTestFactory* old_factory_; DeathTestFactory* old_factory_;
}; };
...@@ -846,8 +847,7 @@ class MacroLogicDeathTest : public testing::Test { ...@@ -846,8 +847,7 @@ class MacroLogicDeathTest : public testing::Test {
static void SetUpTestCase() { static void SetUpTestCase() {
factory_ = new MockDeathTestFactory; factory_ = new MockDeathTestFactory;
replacer_ = new testing::internal::ReplaceDeathTestFactory( replacer_ = new testing::internal::ReplaceDeathTestFactory(factory_);
testing::UnitTest::GetInstance(), factory_);
} }
static void TearDownTestCase() { static void TearDownTestCase() {
...@@ -959,8 +959,7 @@ TEST_F(MacroLogicDeathTest, ChildDoesNotDie) { ...@@ -959,8 +959,7 @@ TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
// Returns the number of successful parts in the current test. // Returns the number of successful parts in the current test.
static size_t GetSuccessfulTestPartCount() { static size_t GetSuccessfulTestPartCount() {
return testing::UnitTest::GetInstance()->impl()->current_test_result()-> return GetUnitTestImpl()->current_test_result()->successful_part_count();
successful_part_count();
} }
// Tests that a successful death test does not register a successful // Tests that a successful death test does not register a successful
......
...@@ -136,6 +136,7 @@ using testing::internal::GetCurrentOsStackTraceExceptTop; ...@@ -136,6 +136,7 @@ using testing::internal::GetCurrentOsStackTraceExceptTop;
using testing::internal::GetFailedPartCount; using testing::internal::GetFailedPartCount;
using testing::internal::GetTestTypeId; using testing::internal::GetTestTypeId;
using testing::internal::GetTypeId; using testing::internal::GetTypeId;
using testing::internal::GetUnitTestImpl;
using testing::internal::GTestFlagSaver; using testing::internal::GTestFlagSaver;
using testing::internal::Int32; using testing::internal::Int32;
using testing::internal::Int32FromEnvOrDie; using testing::internal::Int32FromEnvOrDie;
...@@ -3600,8 +3601,7 @@ TEST(AssertionSyntaxTest, WorksWithConst) { ...@@ -3600,8 +3601,7 @@ TEST(AssertionSyntaxTest, WorksWithConst) {
// Returns the number of successful parts in the current test. // Returns the number of successful parts in the current test.
static size_t GetSuccessfulPartCount() { static size_t GetSuccessfulPartCount() {
return UnitTest::GetInstance()->impl()->current_test_result()-> return GetUnitTestImpl()->current_test_result()->successful_part_count();
successful_part_count();
} }
namespace testing { namespace testing {
...@@ -4416,8 +4416,7 @@ namespace testing { ...@@ -4416,8 +4416,7 @@ namespace testing {
class TestInfoTest : public Test { class TestInfoTest : public Test {
protected: protected:
static TestInfo * GetTestInfo(const char* test_name) { static TestInfo * GetTestInfo(const char* test_name) {
return UnitTest::GetInstance()->impl()-> return GetUnitTestImpl()->GetTestCase("TestInfoTest", "", NULL, NULL)->
GetTestCase("TestInfoTest", "", NULL, NULL)->
GetTestInfo(test_name); GetTestInfo(test_name);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment