diff options
author | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-09-17 19:12:30 (GMT) |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-09-17 19:12:30 (GMT) |
commit | f43e4ff3ad12ace9d423cc3ce02feadb8f24fe67 (patch) | |
tree | 964a05a356fa57618194ac17d3dfd667c744a669 /test | |
parent | 12d740faef11779b27b17c558ca92622bc0d2b54 (diff) | |
download | googletest-f43e4ff3ad12ace9d423cc3ce02feadb8f24fe67.zip googletest-f43e4ff3ad12ace9d423cc3ce02feadb8f24fe67.tar.gz googletest-f43e4ff3ad12ace9d423cc3ce02feadb8f24fe67.tar.bz2 |
Renames the methods in the event listener API, and changes the order of *End events (by Vlad Losev).
Diffstat (limited to 'test')
-rw-r--r-- | test/gtest-listener_test.cc | 191 | ||||
-rw-r--r-- | test/gtest_unittest.cc | 99 |
2 files changed, 219 insertions, 71 deletions
diff --git a/test/gtest-listener_test.cc b/test/gtest-listener_test.cc index 75c2c18..95ff0b1 100644 --- a/test/gtest-listener_test.cc +++ b/test/gtest-listener_test.cc @@ -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 OnTestIterationStart(const UnitTest& /*unit_test*/, + int iteration) { + Message message; + message << GetFullMethodName("OnTestIterationStart") + << "(" << iteration << ")"; + g_events->PushBack(message.GetString()); } - virtual void OnGlobalSetUpStart(const UnitTest& /*unit_test*/) { - g_events->PushBack(String("TestEventListener::OnGlobalSetUpStart")); + virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) { + g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpStart")); } - virtual void OnGlobalSetUpEnd(const UnitTest& /*unit_test*/) { - g_events->PushBack(String("TestEventListener::OnGlobalSetUpEnd")); + 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. diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc index 07e60f5..0cb202c 100644 --- a/test/gtest_unittest.cc +++ b/test/gtest_unittest.cc @@ -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()); + 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()); - ASSERT_STREQ("0", vec.GetElement(0)); - ASSERT_STREQ("1", vec.GetElement(1)); - ASSERT_STREQ("2", vec.GetElement(2)); + 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); } |