gtest-listener_test.cc
Go to the documentation of this file.
00001 // Copyright 2009 Google Inc. All rights reserved.
00002 //
00003 // Redistribution and use in source and binary forms, with or without
00004 // modification, are permitted provided that the following conditions are
00005 // met:
00006 //
00007 //     * Redistributions of source code must retain the above copyright
00008 // notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above
00010 // copyright notice, this list of conditions and the following disclaimer
00011 // in the documentation and/or other materials provided with the
00012 // distribution.
00013 //     * Neither the name of Google Inc. nor the names of its
00014 // contributors may be used to endorse or promote products derived from
00015 // this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 //
00029 // Author: vladl@google.com (Vlad Losev)
00030 //
00031 // The Google C++ Testing Framework (Google Test)
00032 //
00033 // This file verifies Google Test event listeners receive events at the
00034 // right times.
00035 
00036 #include "gtest/gtest.h"
00037 #include <vector>
00038 
00039 using ::testing::AddGlobalTestEnvironment;
00040 using ::testing::Environment;
00041 using ::testing::InitGoogleTest;
00042 using ::testing::Test;
00043 using ::testing::TestCase;
00044 using ::testing::TestEventListener;
00045 using ::testing::TestInfo;
00046 using ::testing::TestPartResult;
00047 using ::testing::UnitTest;
00048 
00049 // Used by tests to register their events.
00050 std::vector<std::string>* g_events = NULL;
00051 
00052 namespace testing {
00053 namespace internal {
00054 
00055 class EventRecordingListener : public TestEventListener {
00056  public:
00057   explicit EventRecordingListener(const char* name) : name_(name) {}
00058 
00059  protected:
00060   virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
00061     g_events->push_back(GetFullMethodName("OnTestProgramStart"));
00062   }
00063 
00064   virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
00065                                     int iteration) {
00066     Message message;
00067     message << GetFullMethodName("OnTestIterationStart")
00068             << "(" << iteration << ")";
00069     g_events->push_back(message.GetString());
00070   }
00071 
00072   virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
00073     g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpStart"));
00074   }
00075 
00076   virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
00077     g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpEnd"));
00078   }
00079 
00080   virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
00081     g_events->push_back(GetFullMethodName("OnTestCaseStart"));
00082   }
00083 
00084   virtual void OnTestStart(const TestInfo& /*test_info*/) {
00085     g_events->push_back(GetFullMethodName("OnTestStart"));
00086   }
00087 
00088   virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
00089     g_events->push_back(GetFullMethodName("OnTestPartResult"));
00090   }
00091 
00092   virtual void OnTestEnd(const TestInfo& /*test_info*/) {
00093     g_events->push_back(GetFullMethodName("OnTestEnd"));
00094   }
00095 
00096   virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
00097     g_events->push_back(GetFullMethodName("OnTestCaseEnd"));
00098   }
00099 
00100   virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
00101     g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownStart"));
00102   }
00103 
00104   virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
00105     g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownEnd"));
00106   }
00107 
00108   virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
00109                                   int iteration) {
00110     Message message;
00111     message << GetFullMethodName("OnTestIterationEnd")
00112             << "("  << iteration << ")";
00113     g_events->push_back(message.GetString());
00114   }
00115 
00116   virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
00117     g_events->push_back(GetFullMethodName("OnTestProgramEnd"));
00118   }
00119 
00120  private:
00121   std::string GetFullMethodName(const char* name) {
00122     return name_ + "." + name;
00123   }
00124 
00125   std::string name_;
00126 };
00127 
00128 class EnvironmentInvocationCatcher : public Environment {
00129  protected:
00130   virtual void SetUp() {
00131     g_events->push_back("Environment::SetUp");
00132   }
00133 
00134   virtual void TearDown() {
00135     g_events->push_back("Environment::TearDown");
00136   }
00137 };
00138 
00139 class ListenerTest : public Test {
00140  protected:
00141   static void SetUpTestCase() {
00142     g_events->push_back("ListenerTest::SetUpTestCase");
00143   }
00144 
00145   static void TearDownTestCase() {
00146     g_events->push_back("ListenerTest::TearDownTestCase");
00147   }
00148 
00149   virtual void SetUp() {
00150     g_events->push_back("ListenerTest::SetUp");
00151   }
00152 
00153   virtual void TearDown() {
00154     g_events->push_back("ListenerTest::TearDown");
00155   }
00156 };
00157 
00158 TEST_F(ListenerTest, DoesFoo) {
00159   // Test execution order within a test case is not guaranteed so we are not
00160   // recording the test name.
00161   g_events->push_back("ListenerTest::* Test Body");
00162   SUCCEED();  // Triggers OnTestPartResult.
00163 }
00164 
00165 TEST_F(ListenerTest, DoesBar) {
00166   g_events->push_back("ListenerTest::* Test Body");
00167   SUCCEED();  // Triggers OnTestPartResult.
00168 }
00169 
00170 }  // namespace internal
00171 
00172 }  // namespace testing
00173 
00174 using ::testing::internal::EnvironmentInvocationCatcher;
00175 using ::testing::internal::EventRecordingListener;
00176 
00177 void VerifyResults(const std::vector<std::string>& data,
00178                    const char* const* expected_data,
00179                    int expected_data_size) {
00180   const int actual_size = data.size();
00181   // If the following assertion fails, a new entry will be appended to
00182   // data.  Hence we save data.size() first.
00183   EXPECT_EQ(expected_data_size, actual_size);
00184 
00185   // Compares the common prefix.
00186   const int shorter_size = expected_data_size <= actual_size ?
00187       expected_data_size : actual_size;
00188   int i = 0;
00189   for (; i < shorter_size; ++i) {
00190     ASSERT_STREQ(expected_data[i], data[i].c_str())
00191         << "at position " << i;
00192   }
00193 
00194   // Prints extra elements in the actual data.
00195   for (; i < actual_size; ++i) {
00196     printf("  Actual event #%d: %s\n", i, data[i].c_str());
00197   }
00198 }
00199 
00200 int main(int argc, char **argv) {
00201   std::vector<std::string> events;
00202   g_events = &events;
00203   InitGoogleTest(&argc, argv);
00204 
00205   UnitTest::GetInstance()->listeners().Append(
00206       new EventRecordingListener("1st"));
00207   UnitTest::GetInstance()->listeners().Append(
00208       new EventRecordingListener("2nd"));
00209 
00210   AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
00211 
00212   GTEST_CHECK_(events.size() == 0)
00213       << "AddGlobalTestEnvironment should not generate any events itself.";
00214 
00215   ::testing::GTEST_FLAG(repeat) = 2;
00216   int ret_val = RUN_ALL_TESTS();
00217 
00218   const char* const expected_events[] = {
00219     "1st.OnTestProgramStart",
00220     "2nd.OnTestProgramStart",
00221     "1st.OnTestIterationStart(0)",
00222     "2nd.OnTestIterationStart(0)",
00223     "1st.OnEnvironmentsSetUpStart",
00224     "2nd.OnEnvironmentsSetUpStart",
00225     "Environment::SetUp",
00226     "2nd.OnEnvironmentsSetUpEnd",
00227     "1st.OnEnvironmentsSetUpEnd",
00228     "1st.OnTestCaseStart",
00229     "2nd.OnTestCaseStart",
00230     "ListenerTest::SetUpTestCase",
00231     "1st.OnTestStart",
00232     "2nd.OnTestStart",
00233     "ListenerTest::SetUp",
00234     "ListenerTest::* Test Body",
00235     "1st.OnTestPartResult",
00236     "2nd.OnTestPartResult",
00237     "ListenerTest::TearDown",
00238     "2nd.OnTestEnd",
00239     "1st.OnTestEnd",
00240     "1st.OnTestStart",
00241     "2nd.OnTestStart",
00242     "ListenerTest::SetUp",
00243     "ListenerTest::* Test Body",
00244     "1st.OnTestPartResult",
00245     "2nd.OnTestPartResult",
00246     "ListenerTest::TearDown",
00247     "2nd.OnTestEnd",
00248     "1st.OnTestEnd",
00249     "ListenerTest::TearDownTestCase",
00250     "2nd.OnTestCaseEnd",
00251     "1st.OnTestCaseEnd",
00252     "1st.OnEnvironmentsTearDownStart",
00253     "2nd.OnEnvironmentsTearDownStart",
00254     "Environment::TearDown",
00255     "2nd.OnEnvironmentsTearDownEnd",
00256     "1st.OnEnvironmentsTearDownEnd",
00257     "2nd.OnTestIterationEnd(0)",
00258     "1st.OnTestIterationEnd(0)",
00259     "1st.OnTestIterationStart(1)",
00260     "2nd.OnTestIterationStart(1)",
00261     "1st.OnEnvironmentsSetUpStart",
00262     "2nd.OnEnvironmentsSetUpStart",
00263     "Environment::SetUp",
00264     "2nd.OnEnvironmentsSetUpEnd",
00265     "1st.OnEnvironmentsSetUpEnd",
00266     "1st.OnTestCaseStart",
00267     "2nd.OnTestCaseStart",
00268     "ListenerTest::SetUpTestCase",
00269     "1st.OnTestStart",
00270     "2nd.OnTestStart",
00271     "ListenerTest::SetUp",
00272     "ListenerTest::* Test Body",
00273     "1st.OnTestPartResult",
00274     "2nd.OnTestPartResult",
00275     "ListenerTest::TearDown",
00276     "2nd.OnTestEnd",
00277     "1st.OnTestEnd",
00278     "1st.OnTestStart",
00279     "2nd.OnTestStart",
00280     "ListenerTest::SetUp",
00281     "ListenerTest::* Test Body",
00282     "1st.OnTestPartResult",
00283     "2nd.OnTestPartResult",
00284     "ListenerTest::TearDown",
00285     "2nd.OnTestEnd",
00286     "1st.OnTestEnd",
00287     "ListenerTest::TearDownTestCase",
00288     "2nd.OnTestCaseEnd",
00289     "1st.OnTestCaseEnd",
00290     "1st.OnEnvironmentsTearDownStart",
00291     "2nd.OnEnvironmentsTearDownStart",
00292     "Environment::TearDown",
00293     "2nd.OnEnvironmentsTearDownEnd",
00294     "1st.OnEnvironmentsTearDownEnd",
00295     "2nd.OnTestIterationEnd(1)",
00296     "1st.OnTestIterationEnd(1)",
00297     "2nd.OnTestProgramEnd",
00298     "1st.OnTestProgramEnd"
00299   };
00300   VerifyResults(events,
00301                 expected_events,
00302                 sizeof(expected_events)/sizeof(expected_events[0]));
00303 
00304   // We need to check manually for ad hoc test failures that happen after
00305   // RUN_ALL_TESTS finishes.
00306   if (UnitTest::GetInstance()->Failed())
00307     ret_val = 1;
00308 
00309   return ret_val;
00310 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03