diff options
author | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-07-16 00:36:55 +0000 |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-07-16 00:36:55 +0000 |
commit | c214ebc830aa918d54e535c6caa2da6317877e12 (patch) | |
tree | f8b2283ea725ae5686723c331341f7f20e375153 /src | |
parent | 3a47ddf8ea888b4e5fe06bf79ef03a1456274f00 (diff) | |
download | googletest-c214ebc830aa918d54e535c6caa2da6317877e12.tar.gz googletest-c214ebc830aa918d54e535c6caa2da6317877e12.tar.bz2 googletest-c214ebc830aa918d54e535c6caa2da6317877e12.zip |
More refactoring for the event listener API, by Vlad Losev.
Diffstat (limited to 'src')
-rw-r--r-- | src/gtest-internal-inl.h | 38 | ||||
-rw-r--r-- | src/gtest.cc | 56 |
2 files changed, 50 insertions, 44 deletions
diff --git a/src/gtest-internal-inl.h b/src/gtest-internal-inl.h index 92975dd0..5bb981d2 100644 --- a/src/gtest-internal-inl.h +++ b/src/gtest-internal-inl.h @@ -483,8 +483,8 @@ class TestInfoImpl { TypeId fixture_class_id() const { return fixture_class_id_; } // Returns the test result. - internal::TestResult* result() { return &result_; } - const internal::TestResult* result() const { return &result_; } + TestResult* result() { return &result_; } + const TestResult* result() const { return &result_; } // Creates the test object, runs it, records its result, and then // deletes it. @@ -520,7 +520,7 @@ class TestInfoImpl { // This field is mutable and needs to be reset before running the // test for the second time. - internal::TestResult result_; + TestResult result_; GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl); }; @@ -742,19 +742,17 @@ class UnitTestImpl { // Returns the TestResult for the test that's currently running, or // the TestResult for the ad hoc test if no test is running. - internal::TestResult* current_test_result(); + TestResult* current_test_result(); // Returns the TestResult for the ad hoc test. - const internal::TestResult* ad_hoc_test_result() const { - return &ad_hoc_test_result_; - } + const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; } // Sets the unit test result printer. // // Does nothing if the input and the current printer object are the // same; otherwise, deletes the old printer object and makes the // input the current printer. - void set_result_printer(UnitTestEventListenerInterface * result_printer); + void set_result_printer(UnitTestEventListenerInterface* result_printer); // Returns the current unit test result printer if it is not NULL; // otherwise, creates an appropriate result printer, makes it the @@ -991,7 +989,7 @@ class UnitTestImpl { // If an assertion is encountered when no TEST or TEST_F is running, // Google Test attributes the assertion result to an imaginary "ad hoc" // test, and records the result in ad_hoc_test_result_. - internal::TestResult ad_hoc_test_result_; + TestResult ad_hoc_test_result_; // The unit test result printer. Will be deleted when the UnitTest // object is destructed. By default, a plain text printer is used, @@ -1122,6 +1120,28 @@ bool ParseNaturalNumber(const ::std::string& str, Integer* number) { } #endif // GTEST_HAS_DEATH_TEST +// TestResult contains some private methods that should be hidden from +// Google Test user but are required for testing. This class allow our tests +// to access them. +class TestResultAccessor { + public: + static void RecordProperty(TestResult* test_result, + const TestProperty& property) { + test_result->RecordProperty(property); + } + + static bool Passed(const TestResult& result) { return result.Passed(); } + + static void ClearTestPartResults(TestResult* test_result) { + test_result->ClearTestPartResults(); + } + + static const Vector<testing::TestPartResult>& test_part_results( + const TestResult& test_result) { + return test_result.test_part_results(); + } +}; + } // namespace internal } // namespace testing diff --git a/src/gtest.cc b/src/gtest.cc index bc970173..69d2517f 100644 --- a/src/gtest.cc +++ b/src/gtest.cc @@ -130,6 +130,8 @@ namespace testing { using internal::TestCase; +using internal::TestProperty; +using internal::TestResult; // Constants. @@ -1831,8 +1833,8 @@ String AppendUserMessage(const String& gtest_msg, // Creates an empty TestResult. TestResult::TestResult() - : test_part_results_(new Vector<TestPartResult>), - test_properties_(new Vector<TestProperty>), + : test_part_results_(new internal::Vector<TestPartResult>), + test_properties_(new internal::Vector<TestProperty>), death_test_count_(0), elapsed_time_(0) { } @@ -1872,9 +1874,10 @@ void TestResult::RecordProperty(const TestProperty& test_property) { if (!ValidateTestProperty(test_property)) { return; } - MutexLock lock(&test_properites_mutex_); + internal::MutexLock lock(&test_properites_mutex_); TestProperty* const property_with_matching_key = - test_properties_->FindIf(TestPropertyKeyIs(test_property.key())); + test_properties_->FindIf( + internal::TestPropertyKeyIs(test_property.key())); if (property_with_matching_key == NULL) { test_properties_->PushBack(test_property); return; @@ -1885,7 +1888,7 @@ void TestResult::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. bool TestResult::ValidateTestProperty(const TestProperty& test_property) { - String key(test_property.key()); + internal::String key(test_property.key()); if (key == "name" || key == "status" || key == "time" || key == "classname") { ADD_FAILURE() << "Reserved key used in RecordProperty(): " @@ -1905,24 +1908,13 @@ void TestResult::Clear() { elapsed_time_ = 0; } -// Returns true iff the test part passed. -static bool TestPartPassed(const TestPartResult & result) { - return result.passed(); -} - -// Gets the number of successful test parts. -int TestResult::successful_part_count() const { - return test_part_results_->CountIf(TestPartPassed); -} - -// Returns true iff the test part failed. -static bool TestPartFailed(const TestPartResult & result) { - return result.failed(); -} - -// Gets the number of failed test parts. -int TestResult::failed_part_count() const { - return test_part_results_->CountIf(TestPartFailed); +// Returns true iff the test failed. +bool TestResult::Failed() const { + for (int i = 0; i < total_part_count(); ++i) { + if (GetTestPartResult(i).failed()) + return true; + } + return false; } // Returns true iff the test part fatally failed. @@ -2264,7 +2256,7 @@ bool TestInfo::should_run() const { return impl_->should_run(); } bool TestInfo::matches_filter() const { return impl_->matches_filter(); } // Returns the result of the test. -const internal::TestResult* TestInfo::result() const { return impl_->result(); } +const TestResult* TestInfo::result() const { return impl_->result(); } // Increments the number of death tests encountered in this test so // far. @@ -3021,7 +3013,7 @@ class XmlUnitTestResultPrinter : public EmptyTestEventListener { // When the String is not empty, it includes a space at the beginning, // to delimit this attribute from prior attributes. static internal::String TestPropertiesAsXmlAttributes( - const internal::TestResult& result); + const TestResult& result); // The output file. const internal::String output_file_; @@ -3160,7 +3152,7 @@ const char* FormatTimeInMillisAsSeconds(TimeInMillis ms) { void XmlUnitTestResultPrinter::PrintXmlTestInfo(FILE* out, const char* test_case_name, const TestInfo& test_info) { - const internal::TestResult& result = *test_info.result(); + const TestResult& result = *test_info.result(); fprintf(out, " <testcase name=\"%s\" status=\"%s\" time=\"%s\" " "classname=\"%s\"%s", @@ -3233,8 +3225,7 @@ void XmlUnitTestResultPrinter::PrintXmlUnitTest(FILE* out, // Produces a string representing the test properties in a result as space // delimited XML attributes based on the property key="value" pairs. internal::String XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes( - const internal::TestResult& result) { - using internal::TestProperty; + const TestResult& result) { Message attributes; for (int i = 0; i < result.test_property_count(); ++i) { const TestProperty& property = result.GetTestProperty(i); @@ -3481,7 +3472,7 @@ void UnitTest::AddTestPartResult(TestPartResultType result_type, // the supplied value already exists, updates its value instead. void UnitTest::RecordPropertyForCurrentTest(const char* key, const char* value) { - const internal::TestProperty test_property(key, value); + const TestProperty test_property(key, value); impl_->current_test_result()->RecordProperty(test_property); } @@ -4089,7 +4080,7 @@ OsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() { // Returns the TestResult for the test that's currently running, or // the TestResult for the ad hoc test if no test is running. -internal::TestResult* UnitTestImpl::current_test_result() { +TestResult* UnitTestImpl::current_test_result() { return current_test_info_ ? current_test_info_->impl()->result() : &ad_hoc_test_result_; } @@ -4136,11 +4127,6 @@ String GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, int skip_count) { return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1); } -// Returns the number of failed test parts in the given test result object. -int GetFailedPartCount(const TestResult* result) { - return result->failed_part_count(); -} - // Used by the GTEST_HIDE_UNREACHABLE_CODE_ macro to suppress unreachable // code warnings. namespace { |