Renames the methods in the event listener API, and changes the order of *End events (by Vlad Losev).

This commit is contained in:
zhanyong.wan 2009-09-17 19:12:30 +00:00
parent 12d740faef
commit f43e4ff3ad
5 changed files with 372 additions and 202 deletions

View File

@ -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.
// 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;
// Called after the test case ends.
// Fired 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.
// 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.

View File

@ -105,7 +105,7 @@ class TersePrinter : public EmptyTestEventListener {
}
// Called after a failed assertion or a SUCCESS().
virtual void OnNewTestPartResult(const TestPartResult& test_part_result) {
virtual void OnTestPartResult(const TestPartResult& test_part_result) {
fprintf(stdout,
"%s in %s:%d\n%s\n",
test_part_result.failed() ? "*** Failure" : "Success",

View File

@ -617,7 +617,7 @@ DefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter(
void DefaultGlobalTestPartResultReporter::ReportTestPartResult(
const TestPartResult& result) {
unit_test_->current_test_result()->AddTestPartResult(result);
unit_test_->listeners()->repeater()->OnNewTestPartResult(result);
unit_test_->listeners()->repeater()->OnTestPartResult(result);
}
DefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter(
@ -2471,12 +2471,10 @@ static void PrintTestPartResult(const TestPartResult& test_part_result) {
// following statements add the test part result message to the Output
// window such that the user can double-click on it to jump to the
// corresponding source code location; otherwise they do nothing.
#ifdef _WIN32_WCE
// Windows Mobile doesn't support the ANSI version of OutputDebugString,
// it works only with UTF16 strings.
::OutputDebugString(internal::String::AnsiToUtf16(result.c_str()));
::OutputDebugString(L"\n");
#elif GTEST_OS_WINDOWS
#if GTEST_OS_WINDOWS && !defined(_WIN32_WCE)
// We don't call OutputDebugString*() on Windows Mobile, as printing
// to stdout is done by OutputDebugString() there already - we don't
// want the same message printed twice.
::OutputDebugStringA(result.c_str());
::OutputDebugStringA("\n");
#endif
@ -2608,17 +2606,19 @@ class PrettyUnitTestResultPrinter : public UnitTestEventListenerInterface {
// The following methods override what's in the
// UnitTestEventListenerInterface class.
virtual void OnUnitTestStart(const UnitTest& unit_test);
virtual void OnGlobalSetUpStart(const UnitTest& unit_test);
virtual void OnGlobalSetUpEnd(const UnitTest& /*unit_test*/) {}
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
virtual void OnEnvironmentsSetUpEnd(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 OnNewTestPartResult(const TestPartResult& result);
virtual void OnTestPartResult(const TestPartResult& result);
virtual void OnTestEnd(const TestInfo& test_info);
virtual void OnGlobalTearDownStart(const UnitTest& unit_test);
virtual void OnGlobalTearDownEnd(const UnitTest& /*unit_test*/) {}
virtual void OnUnitTestEnd(const UnitTest& unit_test);
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*/) {}
private:
static void PrintFailedTests(const UnitTest& unit_test);
@ -2626,8 +2626,12 @@ class PrettyUnitTestResultPrinter : public UnitTestEventListenerInterface {
internal::String test_case_name_;
};
// Called before the unit test starts.
void PrettyUnitTestResultPrinter::OnUnitTestStart(const UnitTest& unit_test) {
// Fired before each iteration of tests starts.
void PrettyUnitTestResultPrinter::OnTestIterationStart(
const UnitTest& unit_test, int iteration) {
if (GTEST_FLAG(repeat) != 1)
printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1);
const char* const filter = GTEST_FLAG(filter).c_str();
// Prints the filter if it's not *. This reminds the user that some
@ -2657,7 +2661,7 @@ void PrettyUnitTestResultPrinter::OnUnitTestStart(const UnitTest& unit_test) {
fflush(stdout);
}
void PrettyUnitTestResultPrinter::OnGlobalSetUpStart(
void PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart(
const UnitTest& /*unit_test*/) {
ColoredPrintf(COLOR_GREEN, "[----------] ");
printf("Global test environment set-up.\n");
@ -2719,7 +2723,7 @@ void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
}
// Called after an assertion failure.
void PrettyUnitTestResultPrinter::OnNewTestPartResult(
void PrettyUnitTestResultPrinter::OnTestPartResult(
const TestPartResult& result) {
// If the test part succeeded, we don't need to do anything.
if (result.type() == TPRT_SUCCESS)
@ -2730,7 +2734,7 @@ void PrettyUnitTestResultPrinter::OnNewTestPartResult(
fflush(stdout);
}
void PrettyUnitTestResultPrinter::OnGlobalTearDownStart(
void PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart(
const UnitTest& /*unit_test*/) {
ColoredPrintf(COLOR_GREEN, "[----------] ");
printf("Global test environment tear-down\n");
@ -2769,7 +2773,8 @@ void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {
}
}
void PrettyUnitTestResultPrinter::OnUnitTestEnd(const UnitTest& unit_test) {
void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
int /*iteration*/) {
ColoredPrintf(COLOR_GREEN, "[==========] ");
printf("%s from %s ran.",
FormatTestCount(unit_test.test_to_run_count()).c_str(),
@ -2808,13 +2813,13 @@ void PrettyUnitTestResultPrinter::OnUnitTestEnd(const UnitTest& unit_test) {
// End PrettyUnitTestResultPrinter
// class UnitTestEventsRepeater
// class TestEventRepeater
//
// This class forwards events to other event listeners.
class UnitTestEventsRepeater : public UnitTestEventListenerInterface {
class TestEventRepeater : public UnitTestEventListenerInterface {
public:
UnitTestEventsRepeater() : forwarding_enabled_(true) {}
virtual ~UnitTestEventsRepeater();
TestEventRepeater() : forwarding_enabled_(true) {}
virtual ~TestEventRepeater();
void Append(UnitTestEventListenerInterface *listener);
UnitTestEventListenerInterface* Release(
UnitTestEventListenerInterface* listener);
@ -2824,17 +2829,19 @@ class UnitTestEventsRepeater : public UnitTestEventListenerInterface {
bool forwarding_enabled() const { return forwarding_enabled_; }
void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; }
virtual void OnUnitTestStart(const UnitTest& unit_test);
virtual void OnUnitTestEnd(const UnitTest& unit_test);
virtual void OnGlobalSetUpStart(const UnitTest& unit_test);
virtual void OnGlobalSetUpEnd(const UnitTest& unit_test);
virtual void OnGlobalTearDownStart(const UnitTest& unit_test);
virtual void OnGlobalTearDownEnd(const UnitTest& unit_test);
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 OnNewTestPartResult(const TestPartResult& result);
virtual void OnTestPartResult(const TestPartResult& result);
private:
// Controls whether events will be forwarded to listeners_. Set to false
@ -2843,21 +2850,21 @@ class UnitTestEventsRepeater : public UnitTestEventListenerInterface {
// The list of listeners that receive events.
Vector<UnitTestEventListenerInterface*> listeners_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestEventsRepeater);
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater);
};
UnitTestEventsRepeater::~UnitTestEventsRepeater() {
TestEventRepeater::~TestEventRepeater() {
for (int i = 0; i < listeners_.size(); i++) {
delete listeners_.GetElement(i);
}
}
void UnitTestEventsRepeater::Append(UnitTestEventListenerInterface *listener) {
void TestEventRepeater::Append(UnitTestEventListenerInterface *listener) {
listeners_.PushBack(listener);
}
// TODO(vladl@google.com): Factor the search functionality into Vector::Find.
UnitTestEventListenerInterface* UnitTestEventsRepeater::Release(
UnitTestEventListenerInterface* TestEventRepeater::Release(
UnitTestEventListenerInterface *listener) {
for (int i = 0; i < listeners_.size(); ++i) {
if (listeners_.GetElement(i) == listener) {
@ -2869,39 +2876,68 @@ UnitTestEventListenerInterface* UnitTestEventsRepeater::Release(
return NULL;
}
// Since the methods are identical, use a macro to reduce boilerplate.
// This defines a member that repeats the call to all listeners.
// Since most methods are very similar, use macros to reduce boilerplate.
// This defines a member that forwards the call to all listeners.
#define GTEST_REPEATER_METHOD_(Name, Type) \
void UnitTestEventsRepeater::Name(const Type& parameter) { \
void TestEventRepeater::Name(const Type& parameter) { \
if (forwarding_enabled_) { \
for (int i = 0; i < listeners_.size(); i++) { \
listeners_.GetElement(i)->Name(parameter); \
} \
} \
}
// This defines a member that forwards the call to all listeners in reverse
// order.
#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \
void TestEventRepeater::Name(const Type& parameter) { \
if (forwarding_enabled_) { \
for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { \
listeners_.GetElement(i)->Name(parameter); \
} \
} \
}
GTEST_REPEATER_METHOD_(OnUnitTestStart, UnitTest)
GTEST_REPEATER_METHOD_(OnUnitTestEnd, UnitTest)
GTEST_REPEATER_METHOD_(OnGlobalSetUpStart, UnitTest)
GTEST_REPEATER_METHOD_(OnGlobalSetUpEnd, UnitTest)
GTEST_REPEATER_METHOD_(OnGlobalTearDownStart, UnitTest)
GTEST_REPEATER_METHOD_(OnGlobalTearDownEnd, UnitTest)
GTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest)
GTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest)
GTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest)
GTEST_REPEATER_METHOD_(OnTestCaseStart, TestCase)
GTEST_REPEATER_METHOD_(OnTestCaseEnd, TestCase)
GTEST_REPEATER_METHOD_(OnTestStart, TestInfo)
GTEST_REPEATER_METHOD_(OnTestEnd, TestInfo)
GTEST_REPEATER_METHOD_(OnNewTestPartResult, TestPartResult)
GTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult)
GTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest)
GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest)
GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest)
GTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestCase)
GTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo)
#undef GTEST_REPEATER_METHOD_
#undef GTEST_REVERSE_REPEATER_METHOD_
// End UnitTestEventsRepeater
void TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test,
int iteration) {
if (forwarding_enabled_) {
for (int i = 0; i < listeners_.size(); i++) {
listeners_.GetElement(i)->OnTestIterationStart(unit_test, iteration);
}
}
}
void TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test,
int iteration) {
if (forwarding_enabled_) {
for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) {
listeners_.GetElement(i)->OnTestIterationEnd(unit_test, iteration);
}
}
}
// End TestEventRepeater
// This class generates an XML output file.
class XmlUnitTestResultPrinter : public EmptyTestEventListener {
public:
explicit XmlUnitTestResultPrinter(const char* output_file);
virtual void OnUnitTestEnd(const UnitTest& unit_test);
virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
private:
// Is c a whitespace character that is normalized to a space character
@ -2963,7 +2999,8 @@ XmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file)
}
// Called after the unit test ends.
void XmlUnitTestResultPrinter::OnUnitTestEnd(const UnitTest& unit_test) {
void XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
int /*iteration*/) {
FILE* xmlout = NULL;
FilePath output_file(output_file_);
FilePath output_dir(output_file.RemoveFileName());
@ -3210,7 +3247,7 @@ OsStackTraceGetter::kElidedFramesMarker =
// class EventListeners
EventListeners::EventListeners()
: repeater_(new internal::UnitTestEventsRepeater()),
: repeater_(new internal::TestEventRepeater()),
default_result_printer_(NULL),
default_xml_generator_(NULL) {
}
@ -3834,31 +3871,27 @@ int UnitTestImpl::RunAllTests() {
UnitTestEventListenerInterface* repeater = listeners()->repeater();
repeater->OnTestProgramStart(*parent_);
// How many times to repeat the tests? We don't want to repeat them
// when we are inside the subprocess of a death test.
const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat);
// Repeats forever if the repeat count is negative.
const bool forever = repeat < 0;
for (int i = 0; forever || i != repeat; i++) {
if (repeat != 1) {
// TODO(vladl@google.com): Move this output to
// PrettyUnitTestResultPrinter. Add the iteration number parameter to
// OnUnitTestStart.
printf("\nRepeating all tests (iteration %d) . . .\n\n", i + 1);
}
ClearResult();
const TimeInMillis start = GetTimeInMillis();
// Tells the unit test event listeners that the tests are about to start.
repeater->OnUnitTestStart(*parent_);
// TODO(vladl@google.com): Move to before the OnUnitTestStart notification?
const TimeInMillis start = GetTimeInMillis();
repeater->OnTestIterationStart(*parent_, i);
// Runs each test case if there is at least one test to run.
if (has_tests_to_run) {
// Sets up all environments beforehand.
repeater->OnGlobalSetUpStart(*parent_);
repeater->OnEnvironmentsSetUpStart(*parent_);
environments_.ForEach(SetUpEnvironment);
repeater->OnGlobalSetUpEnd(*parent_);
repeater->OnEnvironmentsSetUpEnd(*parent_);
// Runs the tests only if there was no fatal failure during global
// set-up.
@ -3867,21 +3900,20 @@ int UnitTestImpl::RunAllTests() {
}
// Tears down all environments in reverse order afterwards.
repeater->OnGlobalTearDownStart(*parent_);
repeater->OnEnvironmentsTearDownStart(*parent_);
environments_in_reverse_order_.ForEach(TearDownEnvironment);
repeater->OnGlobalTearDownEnd(*parent_);
repeater->OnEnvironmentsTearDownEnd(*parent_);
}
elapsed_time_ = GetTimeInMillis() - start;
// Tells the unit test event listener that the tests have just finished.
repeater->OnUnitTestEnd(*parent_);
repeater->OnTestIterationEnd(*parent_, i);
// Gets the result and clears it.
if (!Passed()) {
failed = true;
}
ClearResult();
if (GTEST_FLAG(shuffle)) {
// Picks a new random seed for each run.
@ -3889,6 +3921,8 @@ int UnitTestImpl::RunAllTests() {
}
}
repeater->OnTestProgramEnd(*parent_);
// Returns 0 if all tests passed, or 1 other wise.
return failed ? 1 : 0;
}

View File

@ -73,50 +73,78 @@ class UnitTestAccessor {
};
class EventRecordingListener : public UnitTestEventListenerInterface {
public:
EventRecordingListener(const char* name) : name_(name) {}
protected:
virtual void OnUnitTestStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(String("TestEventListener::OnUnitTestStart"));
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnTestProgramStart"));
}
virtual void OnGlobalSetUpStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(String("TestEventListener::OnGlobalSetUpStart"));
virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
int iteration) {
Message message;
message << GetFullMethodName("OnTestIterationStart")
<< "(" << iteration << ")";
g_events->PushBack(message.GetString());
}
virtual void OnGlobalSetUpEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(String("TestEventListener::OnGlobalSetUpEnd"));
virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpStart"));
}
virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpEnd"));
}
virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
g_events->PushBack(String("TestEventListener::OnTestCaseStart"));
g_events->PushBack(GetFullMethodName("OnTestCaseStart"));
}
virtual void OnTestStart(const TestInfo& /*test_info*/) {
g_events->PushBack(String("TestEventListener::OnTestStart"));
g_events->PushBack(GetFullMethodName("OnTestStart"));
}
virtual void OnNewTestPartResult(const TestPartResult& /*test_part_result*/) {
g_events->PushBack(String("TestEventListener::OnNewTestPartResult"));
virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
g_events->PushBack(GetFullMethodName("OnTestPartResult"));
}
virtual void OnTestEnd(const TestInfo& /*test_info*/) {
g_events->PushBack(String("TestEventListener::OnTestEnd"));
g_events->PushBack(GetFullMethodName("OnTestEnd"));
}
virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
g_events->PushBack(String("TestEventListener::OnTestCaseEnd"));
g_events->PushBack(GetFullMethodName("OnTestCaseEnd"));
}
virtual void OnGlobalTearDownStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(String("TestEventListener::OnGlobalTearDownStart"));
virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownStart"));
}
virtual void OnGlobalTearDownEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(String("TestEventListener::OnGlobalTearDownEnd"));
virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownEnd"));
}
virtual void OnUnitTestEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(String("TestEventListener::OnUnitTestEnd"));
virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
int iteration) {
Message message;
message << GetFullMethodName("OnTestIterationEnd")
<< "(" << iteration << ")";
g_events->PushBack(message.GetString());
}
virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnTestProgramEnd"));
}
private:
String GetFullMethodName(const char* name) {
Message message;
message << name_ << "." << name;
return message.GetString();
}
String name_;
};
class EnvironmentInvocationCatcher : public Environment {
@ -169,57 +197,132 @@ using ::testing::internal::EnvironmentInvocationCatcher;
using ::testing::internal::EventRecordingListener;
using ::testing::internal::UnitTestAccessor;
void VerifyResults(const Vector<String>& data,
const char* const* expected_data,
int expected_data_size) {
const int actual_size = data.size();
// If the following assertion fails, a new entry will be appended to
// data. Hence we save data.size() first.
EXPECT_EQ(expected_data_size, actual_size);
// Compares the common prefix.
const int shorter_size = expected_data_size <= actual_size ?
expected_data_size : actual_size;
int i = 0;
for (; i < shorter_size; ++i) {
ASSERT_STREQ(expected_data[i], data.GetElement(i).c_str())
<< "at position " << i;
}
// Prints extra elements in the actual data.
for (; i < actual_size; ++i) {
printf(" Actual event #%d: %s\n", i, data.GetElement(i).c_str());
}
}
int main(int argc, char **argv) {
Vector<String> events;
g_events = &events;
InitGoogleTest(&argc, argv);
UnitTestEventListenerInterface* listener = new EventRecordingListener;
UnitTestAccessor::GetEventListeners().Append(listener);
UnitTestAccessor::GetEventListeners().Append(
new EventRecordingListener("1st"));
UnitTestAccessor::GetEventListeners().Append(
new EventRecordingListener("2nd"));
AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
GTEST_CHECK_(events.size() == 0)
<< "AddGlobalTestEnvironment should not generate any events itself.";
::testing::GTEST_FLAG(repeat) = 2;
int ret_val = RUN_ALL_TESTS();
const char* const expected_events[] = {
"TestEventListener::OnUnitTestStart",
"TestEventListener::OnGlobalSetUpStart",
"1st.OnTestProgramStart",
"2nd.OnTestProgramStart",
"1st.OnTestIterationStart(0)",
"2nd.OnTestIterationStart(0)",
"1st.OnEnvironmentsSetUpStart",
"2nd.OnEnvironmentsSetUpStart",
"Environment::SetUp",
"TestEventListener::OnGlobalSetUpEnd",
"TestEventListener::OnTestCaseStart",
"2nd.OnEnvironmentsSetUpEnd",
"1st.OnEnvironmentsSetUpEnd",
"1st.OnTestCaseStart",
"2nd.OnTestCaseStart",
"ListenerTest::SetUpTestCase",
"TestEventListener::OnTestStart",
"1st.OnTestStart",
"2nd.OnTestStart",
"ListenerTest::SetUp",
"ListenerTest::* Test Body",
"TestEventListener::OnNewTestPartResult",
"1st.OnTestPartResult",
"2nd.OnTestPartResult",
"ListenerTest::TearDown",
"TestEventListener::OnTestEnd",
"TestEventListener::OnTestStart",
"2nd.OnTestEnd",
"1st.OnTestEnd",
"1st.OnTestStart",
"2nd.OnTestStart",
"ListenerTest::SetUp",
"ListenerTest::* Test Body",
"TestEventListener::OnNewTestPartResult",
"1st.OnTestPartResult",
"2nd.OnTestPartResult",
"ListenerTest::TearDown",
"TestEventListener::OnTestEnd",
"2nd.OnTestEnd",
"1st.OnTestEnd",
"ListenerTest::TearDownTestCase",
"TestEventListener::OnTestCaseEnd",
"TestEventListener::OnGlobalTearDownStart",
"2nd.OnTestCaseEnd",
"1st.OnTestCaseEnd",
"1st.OnEnvironmentsTearDownStart",
"2nd.OnEnvironmentsTearDownStart",
"Environment::TearDown",
"TestEventListener::OnGlobalTearDownEnd",
"TestEventListener::OnUnitTestEnd"
"2nd.OnEnvironmentsTearDownEnd",
"1st.OnEnvironmentsTearDownEnd",
"2nd.OnTestIterationEnd(0)",
"1st.OnTestIterationEnd(0)",
"1st.OnTestIterationStart(1)",
"2nd.OnTestIterationStart(1)",
"1st.OnEnvironmentsSetUpStart",
"2nd.OnEnvironmentsSetUpStart",
"Environment::SetUp",
"2nd.OnEnvironmentsSetUpEnd",
"1st.OnEnvironmentsSetUpEnd",
"1st.OnTestCaseStart",
"2nd.OnTestCaseStart",
"ListenerTest::SetUpTestCase",
"1st.OnTestStart",
"2nd.OnTestStart",
"ListenerTest::SetUp",
"ListenerTest::* Test Body",
"1st.OnTestPartResult",
"2nd.OnTestPartResult",
"ListenerTest::TearDown",
"2nd.OnTestEnd",
"1st.OnTestEnd",
"1st.OnTestStart",
"2nd.OnTestStart",
"ListenerTest::SetUp",
"ListenerTest::* Test Body",
"1st.OnTestPartResult",
"2nd.OnTestPartResult",
"ListenerTest::TearDown",
"2nd.OnTestEnd",
"1st.OnTestEnd",
"ListenerTest::TearDownTestCase",
"2nd.OnTestCaseEnd",
"1st.OnTestCaseEnd",
"1st.OnEnvironmentsTearDownStart",
"2nd.OnEnvironmentsTearDownStart",
"Environment::TearDown",
"2nd.OnEnvironmentsTearDownEnd",
"1st.OnEnvironmentsTearDownEnd",
"2nd.OnTestIterationEnd(1)",
"1st.OnTestIterationEnd(1)",
"2nd.OnTestProgramEnd",
"1st.OnTestProgramEnd"
};
const int kExpectedEventsSize =
sizeof(expected_events)/sizeof(expected_events[0]);
// Cannot use ASSERT_EQ() here because it requires the scoping function to
// return void.
GTEST_CHECK_(events.size() == kExpectedEventsSize);
for (int i = 0; i < events.size(); ++i)
GTEST_CHECK_(String(events.GetElement(i)) == expected_events[i])
<< "At position " << i;
VerifyResults(events,
expected_events,
sizeof(expected_events)/sizeof(expected_events[0]));
// We need to check manually for ad hoc test failures that happen after
// RUN_ALL_TESTS finishes.

View File

@ -6215,7 +6215,7 @@ class TestListener : public EmptyTestEventListener {
}
protected:
virtual void OnUnitTestStart(const UnitTest& /*unit_test*/) {
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
if (on_start_counter_ != NULL)
(*on_start_counter_)++;
}
@ -6269,43 +6269,88 @@ TEST(EventListenersTest, Append) {
{
EventListeners listeners;
listeners.Append(listener);
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(1, on_start_counter);
}
EXPECT_TRUE(is_destroyed);
}
// Tests that listeners receive requests in the order they were appended to
// the list.
// Tests that listeners receive events in the order they were appended to
// the list, except for *End requests, which must be received in the reverse
// order.
class SequenceTestingListener : public EmptyTestEventListener {
public:
SequenceTestingListener(Vector<const char*>* vector, const char* signature)
: vector_(vector), signature_(signature) {}
SequenceTestingListener(Vector<String>* vector, const char* id)
: vector_(vector), id_(id) {}
protected:
virtual void OnUnitTestStart(const UnitTest& /*unit_test*/) {
if (vector_ != NULL)
vector_->PushBack(signature_);
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
vector_->PushBack(GetEventDescription("OnTestProgramStart"));
}
virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
vector_->PushBack(GetEventDescription("OnTestProgramEnd"));
}
virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
int /*iteration*/) {
vector_->PushBack(GetEventDescription("OnTestIterationStart"));
}
virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
int /*iteration*/) {
vector_->PushBack(GetEventDescription("OnTestIterationEnd"));
}
private:
Vector<const char*>* vector_;
const char* const signature_;
String GetEventDescription(const char* method) {
Message message;
message << id_ << "." << method;
return message.GetString();
}
Vector<String>* vector_;
const char* const id_;
};
TEST(EventListenerTest, AppendKeepsOrder) {
Vector<const char*> vec;
Vector<String> vec;
EventListeners listeners;
listeners.Append(new SequenceTestingListener(&vec, "0"));
listeners.Append(new SequenceTestingListener(&vec, "1"));
listeners.Append(new SequenceTestingListener(&vec, "2"));
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
listeners.Append(new SequenceTestingListener(&vec, "1st"));
listeners.Append(new SequenceTestingListener(&vec, "2nd"));
listeners.Append(new SequenceTestingListener(&vec, "3rd"));
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
ASSERT_EQ(3, vec.size());
ASSERT_STREQ("0", vec.GetElement(0));
ASSERT_STREQ("1", vec.GetElement(1));
ASSERT_STREQ("2", vec.GetElement(2));
EXPECT_STREQ("1st.OnTestProgramStart", vec.GetElement(0).c_str());
EXPECT_STREQ("2nd.OnTestProgramStart", vec.GetElement(1).c_str());
EXPECT_STREQ("3rd.OnTestProgramStart", vec.GetElement(2).c_str());
vec.Clear();
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramEnd(
*UnitTest::GetInstance());
ASSERT_EQ(3, vec.size());
EXPECT_STREQ("3rd.OnTestProgramEnd", vec.GetElement(0).c_str());
EXPECT_STREQ("2nd.OnTestProgramEnd", vec.GetElement(1).c_str());
EXPECT_STREQ("1st.OnTestProgramEnd", vec.GetElement(2).c_str());
vec.Clear();
EventListenersAccessor::GetRepeater(&listeners)->OnTestIterationStart(
*UnitTest::GetInstance(), 0);
ASSERT_EQ(3, vec.size());
EXPECT_STREQ("1st.OnTestIterationStart", vec.GetElement(0).c_str());
EXPECT_STREQ("2nd.OnTestIterationStart", vec.GetElement(1).c_str());
EXPECT_STREQ("3rd.OnTestIterationStart", vec.GetElement(2).c_str());
vec.Clear();
EventListenersAccessor::GetRepeater(&listeners)->OnTestIterationEnd(
*UnitTest::GetInstance(), 0);
ASSERT_EQ(3, vec.size());
EXPECT_STREQ("3rd.OnTestIterationEnd", vec.GetElement(0).c_str());
EXPECT_STREQ("2nd.OnTestIterationEnd", vec.GetElement(1).c_str());
EXPECT_STREQ("1st.OnTestIterationEnd", vec.GetElement(2).c_str());
}
// Tests that a listener removed from an EventListeners list stops receiving
@ -6321,7 +6366,7 @@ TEST(EventListenersTest, Release) {
EventListeners listeners;
listeners.Append(listener);
EXPECT_EQ(listener, listeners.Release(listener));
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_TRUE(listeners.Release(listener) == NULL);
}
@ -6340,7 +6385,7 @@ TEST(EventListenerTest, SuppressEventForwarding) {
ASSERT_TRUE(EventListenersAccessor::EventForwardingEnabled(listeners));
EventListenersAccessor::SuppressEventForwarding(&listeners);
ASSERT_FALSE(EventListenersAccessor::EventForwardingEnabled(listeners));
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(0, on_start_counter);
}
@ -6367,7 +6412,7 @@ TEST(EventListenerTest, default_result_printer) {
EXPECT_EQ(listener, listeners.default_result_printer());
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(1, on_start_counter);
@ -6381,7 +6426,7 @@ TEST(EventListenerTest, default_result_printer) {
// After broadcasting an event the counter is still the same, indicating
// the listener is not in the list anymore.
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(1, on_start_counter);
}
@ -6404,7 +6449,7 @@ TEST(EventListenerTest, RemovingDefaultResultPrinterWorks) {
EXPECT_FALSE(is_destroyed);
// Broadcasting events now should not affect default_result_printer.
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(0, on_start_counter);
}
@ -6426,7 +6471,7 @@ TEST(EventListenerTest, default_xml_generator) {
EXPECT_EQ(listener, listeners.default_xml_generator());
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(1, on_start_counter);
@ -6440,7 +6485,7 @@ TEST(EventListenerTest, default_xml_generator) {
// After broadcasting an event the counter is still the same, indicating
// the listener is not in the list anymore.
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(1, on_start_counter);
}
@ -6463,7 +6508,7 @@ TEST(EventListenerTest, RemovingDefaultXmlGeneratorWorks) {
EXPECT_FALSE(is_destroyed);
// Broadcasting events now should not affect default_xml_generator.
EventListenersAccessor::GetRepeater(&listeners)->OnUnitTestStart(
EventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
*UnitTest::GetInstance());
EXPECT_EQ(0, on_start_counter);
}