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 
     38 // Indicates that this translation unit is part of Google Test's
     39 // implementation.  It must come before gtest-internal-inl.h is
     40 // included, or there will be a compiler error.  This trick is to
     41 // prevent a user from accidentally including gtest-internal-inl.h in
     42 // his code.
     43 #define GTEST_IMPLEMENTATION_ 1
     44 #include "src/gtest-internal-inl.h"  // For Vector.
     45 #undef GTEST_IMPLEMENTATION_
     46 
     47 using ::testing::AddGlobalTestEnvironment;
     48 using ::testing::Environment;
     49 using ::testing::InitGoogleTest;
     50 using ::testing::Test;
     51 using ::testing::TestCase;
     52 using ::testing::TestEventListener;
     53 using ::testing::TestInfo;
     54 using ::testing::TestPartResult;
     55 using ::testing::UnitTest;
     56 using ::testing::internal::String;
     57 using ::testing::internal::Vector;
     58 
     59 // Used by tests to register their events.
     60 Vector<String>* g_events = NULL;
     61 
     62 namespace testing {
     63 namespace internal {
     64 
     65 class EventRecordingListener : public TestEventListener {
     66  public:
     67   EventRecordingListener(const char* name) : name_(name) {}
     68 
     69  protected:
     70   virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
     71     g_events->PushBack(GetFullMethodName("OnTestProgramStart"));
     72   }
     73 
     74   virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
     75                                     int iteration) {
     76     Message message;
     77     message << GetFullMethodName("OnTestIterationStart")
     78             << "(" << iteration << ")";
     79     g_events->PushBack(message.GetString());
     80   }
     81 
     82   virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
     83     g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpStart"));
     84   }
     85 
     86   virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
     87     g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpEnd"));
     88   }
     89 
     90   virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
     91     g_events->PushBack(GetFullMethodName("OnTestCaseStart"));
     92   }
     93 
     94   virtual void OnTestStart(const TestInfo& /*test_info*/) {
     95     g_events->PushBack(GetFullMethodName("OnTestStart"));
     96   }
     97 
     98   virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
     99     g_events->PushBack(GetFullMethodName("OnTestPartResult"));
    100   }
    101 
    102   virtual void OnTestEnd(const TestInfo& /*test_info*/) {
    103     g_events->PushBack(GetFullMethodName("OnTestEnd"));
    104   }
    105 
    106   virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
    107     g_events->PushBack(GetFullMethodName("OnTestCaseEnd"));
    108   }
    109 
    110   virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
    111     g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownStart"));
    112   }
    113 
    114   virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
    115     g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownEnd"));
    116   }
    117 
    118   virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
    119                                   int iteration) {
    120     Message message;
    121     message << GetFullMethodName("OnTestIterationEnd")
    122             << "("  << iteration << ")";
    123     g_events->PushBack(message.GetString());
    124   }
    125 
    126   virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
    127     g_events->PushBack(GetFullMethodName("OnTestProgramEnd"));
    128   }
    129 
    130  private:
    131   String GetFullMethodName(const char* name) {
    132     Message message;
    133     message << name_ << "." << name;
    134     return message.GetString();
    135   }
    136 
    137   String name_;
    138 };
    139 
    140 class EnvironmentInvocationCatcher : public Environment {
    141  protected:
    142   virtual void SetUp() {
    143     g_events->PushBack(String("Environment::SetUp"));
    144   }
    145 
    146   virtual void TearDown() {
    147     g_events->PushBack(String("Environment::TearDown"));
    148   }
    149 };
    150 
    151 class ListenerTest : public Test {
    152  protected:
    153   static void SetUpTestCase() {
    154     g_events->PushBack(String("ListenerTest::SetUpTestCase"));
    155   }
    156 
    157   static void TearDownTestCase() {
    158     g_events->PushBack(String("ListenerTest::TearDownTestCase"));
    159   }
    160 
    161   virtual void SetUp() {
    162     g_events->PushBack(String("ListenerTest::SetUp"));
    163   }
    164 
    165   virtual void TearDown() {
    166     g_events->PushBack(String("ListenerTest::TearDown"));
    167   }
    168 };
    169 
    170 TEST_F(ListenerTest, DoesFoo) {
    171   // Test execution order within a test case is not guaranteed so we are not
    172   // recording the test name.
    173   g_events->PushBack(String("ListenerTest::* Test Body"));
    174   SUCCEED();  // Triggers OnTestPartResult.
    175 }
    176 
    177 TEST_F(ListenerTest, DoesBar) {
    178   g_events->PushBack(String("ListenerTest::* Test Body"));
    179   SUCCEED();  // Triggers OnTestPartResult.
    180 }
    181 
    182 }  // namespace internal
    183 
    184 }  // namespace testing
    185 
    186 using ::testing::internal::EnvironmentInvocationCatcher;
    187 using ::testing::internal::EventRecordingListener;
    188 
    189 void VerifyResults(const Vector<String>& data,
    190                    const char* const* expected_data,
    191                    int expected_data_size) {
    192   const int actual_size = data.size();
    193   // If the following assertion fails, a new entry will be appended to
    194   // data.  Hence we save data.size() first.
    195   EXPECT_EQ(expected_data_size, actual_size);
    196 
    197   // Compares the common prefix.
    198   const int shorter_size = expected_data_size <= actual_size ?
    199       expected_data_size : actual_size;
    200   int i = 0;
    201   for (; i < shorter_size; ++i) {
    202     ASSERT_STREQ(expected_data[i], data.GetElement(i).c_str())
    203         << "at position " << i;
    204   }
    205 
    206   // Prints extra elements in the actual data.
    207   for (; i < actual_size; ++i) {
    208     printf("  Actual event #%d: %s\n", i, data.GetElement(i).c_str());
    209   }
    210 }
    211 
    212 int main(int argc, char **argv) {
    213   Vector<String> events;
    214   g_events = &events;
    215   InitGoogleTest(&argc, argv);
    216 
    217   UnitTest::GetInstance()->listeners().Append(
    218       new EventRecordingListener("1st"));
    219   UnitTest::GetInstance()->listeners().Append(
    220       new EventRecordingListener("2nd"));
    221 
    222   AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
    223 
    224   GTEST_CHECK_(events.size() == 0)
    225       << "AddGlobalTestEnvironment should not generate any events itself.";
    226 
    227   ::testing::GTEST_FLAG(repeat) = 2;
    228   int ret_val = RUN_ALL_TESTS();
    229 
    230   const char* const expected_events[] = {
    231     "1st.OnTestProgramStart",
    232     "2nd.OnTestProgramStart",
    233     "1st.OnTestIterationStart(0)",
    234     "2nd.OnTestIterationStart(0)",
    235     "1st.OnEnvironmentsSetUpStart",
    236     "2nd.OnEnvironmentsSetUpStart",
    237     "Environment::SetUp",
    238     "2nd.OnEnvironmentsSetUpEnd",
    239     "1st.OnEnvironmentsSetUpEnd",
    240     "1st.OnTestCaseStart",
    241     "2nd.OnTestCaseStart",
    242     "ListenerTest::SetUpTestCase",
    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     "1st.OnTestStart",
    253     "2nd.OnTestStart",
    254     "ListenerTest::SetUp",
    255     "ListenerTest::* Test Body",
    256     "1st.OnTestPartResult",
    257     "2nd.OnTestPartResult",
    258     "ListenerTest::TearDown",
    259     "2nd.OnTestEnd",
    260     "1st.OnTestEnd",
    261     "ListenerTest::TearDownTestCase",
    262     "2nd.OnTestCaseEnd",
    263     "1st.OnTestCaseEnd",
    264     "1st.OnEnvironmentsTearDownStart",
    265     "2nd.OnEnvironmentsTearDownStart",
    266     "Environment::TearDown",
    267     "2nd.OnEnvironmentsTearDownEnd",
    268     "1st.OnEnvironmentsTearDownEnd",
    269     "2nd.OnTestIterationEnd(0)",
    270     "1st.OnTestIterationEnd(0)",
    271     "1st.OnTestIterationStart(1)",
    272     "2nd.OnTestIterationStart(1)",
    273     "1st.OnEnvironmentsSetUpStart",
    274     "2nd.OnEnvironmentsSetUpStart",
    275     "Environment::SetUp",
    276     "2nd.OnEnvironmentsSetUpEnd",
    277     "1st.OnEnvironmentsSetUpEnd",
    278     "1st.OnTestCaseStart",
    279     "2nd.OnTestCaseStart",
    280     "ListenerTest::SetUpTestCase",
    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     "1st.OnTestStart",
    291     "2nd.OnTestStart",
    292     "ListenerTest::SetUp",
    293     "ListenerTest::* Test Body",
    294     "1st.OnTestPartResult",
    295     "2nd.OnTestPartResult",
    296     "ListenerTest::TearDown",
    297     "2nd.OnTestEnd",
    298     "1st.OnTestEnd",
    299     "ListenerTest::TearDownTestCase",
    300     "2nd.OnTestCaseEnd",
    301     "1st.OnTestCaseEnd",
    302     "1st.OnEnvironmentsTearDownStart",
    303     "2nd.OnEnvironmentsTearDownStart",
    304     "Environment::TearDown",
    305     "2nd.OnEnvironmentsTearDownEnd",
    306     "1st.OnEnvironmentsTearDownEnd",
    307     "2nd.OnTestIterationEnd(1)",
    308     "1st.OnTestIterationEnd(1)",
    309     "2nd.OnTestProgramEnd",
    310     "1st.OnTestProgramEnd"
    311   };
    312   VerifyResults(events,
    313                 expected_events,
    314                 sizeof(expected_events)/sizeof(expected_events[0]));
    315 
    316   // We need to check manually for ad hoc test failures that happen after
    317   // RUN_ALL_TESTS finishes.
    318   if (UnitTest::GetInstance()->Failed())
    319     ret_val = 1;
    320 
    321   return ret_val;
    322 }
    323