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