Home | History | Annotate | Download | only in test
      1 // Copyright 2009 Google Inc. All rights reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //     * Redistributions of source code must retain the above copyright
      8 // notice, this list of conditions and the following disclaimer.
      9 //     * Redistributions in binary form must reproduce the above
     10 // copyright notice, this list of conditions and the following disclaimer
     11 // in the documentation and/or other materials provided with the
     12 // distribution.
     13 //     * Neither the name of Google Inc. nor the names of its
     14 // contributors may be used to endorse or promote products derived from
     15 // this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 //
     29 // Author: vladl (at) google.com (Vlad Losev)
     30 //
     31 // The Google C++ Testing Framework (Google Test)
     32 //
     33 // This file verifies Google Test event listeners receive events at the
     34 // right times.
     35 
     36 #include "gtest/gtest.h"
     37 #include <vector>
     38 
     39 using ::testing::AddGlobalTestEnvironment;
     40 using ::testing::Environment;
     41 using ::testing::InitGoogleTest;
     42 using ::testing::Test;
     43 using ::testing::TestCase;
     44 using ::testing::TestEventListener;
     45 using ::testing::TestInfo;
     46 using ::testing::TestPartResult;
     47 using ::testing::UnitTest;
     48 using ::testing::internal::String;
     49 
     50 // Used by tests to register their events.
     51 std::vector<String>* g_events = NULL;
     52 
     53 namespace testing {
     54 namespace internal {
     55 
     56 class EventRecordingListener : public TestEventListener {
     57  public:
     58   EventRecordingListener(const char* name) : name_(name) {}
     59 
     60  protected:
     61   virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
     62     g_events->push_back(GetFullMethodName("OnTestProgramStart"));
     63   }
     64 
     65   virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
     66                                     int iteration) {
     67     Message message;
     68     message << GetFullMethodName("OnTestIterationStart")
     69             << "(" << iteration << ")";
     70     g_events->push_back(message.GetString());
     71   }
     72 
     73   virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
     74     g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpStart"));
     75   }
     76 
     77   virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
     78     g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpEnd"));
     79   }
     80 
     81   virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
     82     g_events->push_back(GetFullMethodName("OnTestCaseStart"));
     83   }
     84 
     85   virtual void OnTestStart(const TestInfo& /*test_info*/) {
     86     g_events->push_back(GetFullMethodName("OnTestStart"));
     87   }
     88 
     89   virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
     90     g_events->push_back(GetFullMethodName("OnTestPartResult"));
     91   }
     92 
     93   virtual void OnTestEnd(const TestInfo& /*test_info*/) {
     94     g_events->push_back(GetFullMethodName("OnTestEnd"));
     95   }
     96 
     97   virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
     98     g_events->push_back(GetFullMethodName("OnTestCaseEnd"));
     99   }
    100 
    101   virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
    102     g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownStart"));
    103   }
    104 
    105   virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
    106     g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownEnd"));
    107   }
    108 
    109   virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
    110                                   int iteration) {
    111     Message message;
    112     message << GetFullMethodName("OnTestIterationEnd")
    113             << "("  << iteration << ")";
    114     g_events->push_back(message.GetString());
    115   }
    116 
    117   virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
    118     g_events->push_back(GetFullMethodName("OnTestProgramEnd"));
    119   }
    120 
    121  private:
    122   String GetFullMethodName(const char* name) {
    123     Message message;
    124     message << name_ << "." << name;
    125     return message.GetString();
    126   }
    127 
    128   String name_;
    129 };
    130 
    131 class EnvironmentInvocationCatcher : public Environment {
    132  protected:
    133   virtual void SetUp() {
    134     g_events->push_back(String("Environment::SetUp"));
    135   }
    136 
    137   virtual void TearDown() {
    138     g_events->push_back(String("Environment::TearDown"));
    139   }
    140 };
    141 
    142 class ListenerTest : public Test {
    143  protected:
    144   static void SetUpTestCase() {
    145     g_events->push_back(String("ListenerTest::SetUpTestCase"));
    146   }
    147 
    148   static void TearDownTestCase() {
    149     g_events->push_back(String("ListenerTest::TearDownTestCase"));
    150   }
    151 
    152   virtual void SetUp() {
    153     g_events->push_back(String("ListenerTest::SetUp"));
    154   }
    155 
    156   virtual void TearDown() {
    157     g_events->push_back(String("ListenerTest::TearDown"));
    158   }
    159 };
    160 
    161 TEST_F(ListenerTest, DoesFoo) {
    162   // Test execution order within a test case is not guaranteed so we are not
    163   // recording the test name.
    164   g_events->push_back(String("ListenerTest::* Test Body"));
    165   SUCCEED();  // Triggers OnTestPartResult.
    166 }
    167 
    168 TEST_F(ListenerTest, DoesBar) {
    169   g_events->push_back(String("ListenerTest::* Test Body"));
    170   SUCCEED();  // Triggers OnTestPartResult.
    171 }
    172 
    173 }  // namespace internal
    174 
    175 }  // namespace testing
    176 
    177 using ::testing::internal::EnvironmentInvocationCatcher;
    178 using ::testing::internal::EventRecordingListener;
    179 
    180 void VerifyResults(const std::vector<String>& data,
    181                    const char* const* expected_data,
    182                    int expected_data_size) {
    183   const int actual_size = data.size();
    184   // If the following assertion fails, a new entry will be appended to
    185   // data.  Hence we save data.size() first.
    186   EXPECT_EQ(expected_data_size, actual_size);
    187 
    188   // Compares the common prefix.
    189   const int shorter_size = expected_data_size <= actual_size ?
    190       expected_data_size : actual_size;
    191   int i = 0;
    192   for (; i < shorter_size; ++i) {
    193     ASSERT_STREQ(expected_data[i], data[i].c_str())
    194         << "at position " << i;
    195   }
    196 
    197   // Prints extra elements in the actual data.
    198   for (; i < actual_size; ++i) {
    199     printf("  Actual event #%d: %s\n", i, data[i].c_str());
    200   }
    201 }
    202 
    203 int main(int argc, char **argv) {
    204   std::vector<String> events;
    205   g_events = &events;
    206   InitGoogleTest(&argc, argv);
    207 
    208   UnitTest::GetInstance()->listeners().Append(
    209       new EventRecordingListener("1st"));
    210   UnitTest::GetInstance()->listeners().Append(
    211       new EventRecordingListener("2nd"));
    212 
    213   AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
    214 
    215   GTEST_CHECK_(events.size() == 0)
    216       << "AddGlobalTestEnvironment should not generate any events itself.";
    217 
    218   ::testing::GTEST_FLAG(repeat) = 2;
    219   int ret_val = RUN_ALL_TESTS();
    220 
    221   const char* const expected_events[] = {
    222     "1st.OnTestProgramStart",
    223     "2nd.OnTestProgramStart",
    224     "1st.OnTestIterationStart(0)",
    225     "2nd.OnTestIterationStart(0)",
    226     "1st.OnEnvironmentsSetUpStart",
    227     "2nd.OnEnvironmentsSetUpStart",
    228     "Environment::SetUp",
    229     "2nd.OnEnvironmentsSetUpEnd",
    230     "1st.OnEnvironmentsSetUpEnd",
    231     "1st.OnTestCaseStart",
    232     "2nd.OnTestCaseStart",
    233     "ListenerTest::SetUpTestCase",
    234     "1st.OnTestStart",
    235     "2nd.OnTestStart",
    236     "ListenerTest::SetUp",
    237     "ListenerTest::* Test Body",
    238     "1st.OnTestPartResult",
    239     "2nd.OnTestPartResult",
    240     "ListenerTest::TearDown",
    241     "2nd.OnTestEnd",
    242     "1st.OnTestEnd",
    243     "1st.OnTestStart",
    244     "2nd.OnTestStart",
    245     "ListenerTest::SetUp",
    246     "ListenerTest::* Test Body",
    247     "1st.OnTestPartResult",
    248     "2nd.OnTestPartResult",
    249     "ListenerTest::TearDown",
    250     "2nd.OnTestEnd",
    251     "1st.OnTestEnd",
    252     "ListenerTest::TearDownTestCase",
    253     "2nd.OnTestCaseEnd",
    254     "1st.OnTestCaseEnd",
    255     "1st.OnEnvironmentsTearDownStart",
    256     "2nd.OnEnvironmentsTearDownStart",
    257     "Environment::TearDown",
    258     "2nd.OnEnvironmentsTearDownEnd",
    259     "1st.OnEnvironmentsTearDownEnd",
    260     "2nd.OnTestIterationEnd(0)",
    261     "1st.OnTestIterationEnd(0)",
    262     "1st.OnTestIterationStart(1)",
    263     "2nd.OnTestIterationStart(1)",
    264     "1st.OnEnvironmentsSetUpStart",
    265     "2nd.OnEnvironmentsSetUpStart",
    266     "Environment::SetUp",
    267     "2nd.OnEnvironmentsSetUpEnd",
    268     "1st.OnEnvironmentsSetUpEnd",
    269     "1st.OnTestCaseStart",
    270     "2nd.OnTestCaseStart",
    271     "ListenerTest::SetUpTestCase",
    272     "1st.OnTestStart",
    273     "2nd.OnTestStart",
    274     "ListenerTest::SetUp",
    275     "ListenerTest::* Test Body",
    276     "1st.OnTestPartResult",
    277     "2nd.OnTestPartResult",
    278     "ListenerTest::TearDown",
    279     "2nd.OnTestEnd",
    280     "1st.OnTestEnd",
    281     "1st.OnTestStart",
    282     "2nd.OnTestStart",
    283     "ListenerTest::SetUp",
    284     "ListenerTest::* Test Body",
    285     "1st.OnTestPartResult",
    286     "2nd.OnTestPartResult",
    287     "ListenerTest::TearDown",
    288     "2nd.OnTestEnd",
    289     "1st.OnTestEnd",
    290     "ListenerTest::TearDownTestCase",
    291     "2nd.OnTestCaseEnd",
    292     "1st.OnTestCaseEnd",
    293     "1st.OnEnvironmentsTearDownStart",
    294     "2nd.OnEnvironmentsTearDownStart",
    295     "Environment::TearDown",
    296     "2nd.OnEnvironmentsTearDownEnd",
    297     "1st.OnEnvironmentsTearDownEnd",
    298     "2nd.OnTestIterationEnd(1)",
    299     "1st.OnTestIterationEnd(1)",
    300     "2nd.OnTestProgramEnd",
    301     "1st.OnTestProgramEnd"
    302   };
    303   VerifyResults(events,
    304                 expected_events,
    305                 sizeof(expected_events)/sizeof(expected_events[0]));
    306 
    307   // We need to check manually for ad hoc test failures that happen after
    308   // RUN_ALL_TESTS finishes.
    309   if (UnitTest::GetInstance()->Failed())
    310     ret_val = 1;
    311 
    312   return ret_val;
    313 }
    314