90747685f072b22108fcd1c31d5ccc5d46229630
[platform/upstream/gtest.git] / test / gtest-listener_test.cc
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
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
49 // Used by tests to register their events.
50 std::vector<std::string>* g_events = NULL;
51
52 namespace testing {
53 namespace internal {
54
55 class 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
128 class 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
139 class 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
158 TEST_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
165 TEST_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
174 using ::testing::internal::EnvironmentInvocationCatcher;
175 using ::testing::internal::EventRecordingListener;
176
177 void VerifyResults(const std::vector<std::string>& data,
178                    const char* const* expected_data,
179                    size_t expected_data_size) {
180   const size_t 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 size_t shorter_size = expected_data_size <= actual_size ?
187       expected_data_size : actual_size;
188   size_t 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 #%lu: %s\n",
197         static_cast<unsigned long>(i), data[i].c_str());
198   }
199 }
200
201 int main(int argc, char **argv) {
202   std::vector<std::string> events;
203   g_events = &events;
204   InitGoogleTest(&argc, argv);
205
206   UnitTest::GetInstance()->listeners().Append(
207       new EventRecordingListener("1st"));
208   UnitTest::GetInstance()->listeners().Append(
209       new EventRecordingListener("2nd"));
210
211   AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
212
213   GTEST_CHECK_(events.size() == 0)
214       << "AddGlobalTestEnvironment should not generate any events itself.";
215
216   ::testing::GTEST_FLAG(repeat) = 2;
217   int ret_val = RUN_ALL_TESTS();
218
219   const char* const expected_events[] = {
220     "1st.OnTestProgramStart",
221     "2nd.OnTestProgramStart",
222     "1st.OnTestIterationStart(0)",
223     "2nd.OnTestIterationStart(0)",
224     "1st.OnEnvironmentsSetUpStart",
225     "2nd.OnEnvironmentsSetUpStart",
226     "Environment::SetUp",
227     "2nd.OnEnvironmentsSetUpEnd",
228     "1st.OnEnvironmentsSetUpEnd",
229     "1st.OnTestCaseStart",
230     "2nd.OnTestCaseStart",
231     "ListenerTest::SetUpTestCase",
232     "1st.OnTestStart",
233     "2nd.OnTestStart",
234     "ListenerTest::SetUp",
235     "ListenerTest::* Test Body",
236     "1st.OnTestPartResult",
237     "2nd.OnTestPartResult",
238     "ListenerTest::TearDown",
239     "2nd.OnTestEnd",
240     "1st.OnTestEnd",
241     "1st.OnTestStart",
242     "2nd.OnTestStart",
243     "ListenerTest::SetUp",
244     "ListenerTest::* Test Body",
245     "1st.OnTestPartResult",
246     "2nd.OnTestPartResult",
247     "ListenerTest::TearDown",
248     "2nd.OnTestEnd",
249     "1st.OnTestEnd",
250     "ListenerTest::TearDownTestCase",
251     "2nd.OnTestCaseEnd",
252     "1st.OnTestCaseEnd",
253     "1st.OnEnvironmentsTearDownStart",
254     "2nd.OnEnvironmentsTearDownStart",
255     "Environment::TearDown",
256     "2nd.OnEnvironmentsTearDownEnd",
257     "1st.OnEnvironmentsTearDownEnd",
258     "2nd.OnTestIterationEnd(0)",
259     "1st.OnTestIterationEnd(0)",
260     "1st.OnTestIterationStart(1)",
261     "2nd.OnTestIterationStart(1)",
262     "1st.OnEnvironmentsSetUpStart",
263     "2nd.OnEnvironmentsSetUpStart",
264     "Environment::SetUp",
265     "2nd.OnEnvironmentsSetUpEnd",
266     "1st.OnEnvironmentsSetUpEnd",
267     "1st.OnTestCaseStart",
268     "2nd.OnTestCaseStart",
269     "ListenerTest::SetUpTestCase",
270     "1st.OnTestStart",
271     "2nd.OnTestStart",
272     "ListenerTest::SetUp",
273     "ListenerTest::* Test Body",
274     "1st.OnTestPartResult",
275     "2nd.OnTestPartResult",
276     "ListenerTest::TearDown",
277     "2nd.OnTestEnd",
278     "1st.OnTestEnd",
279     "1st.OnTestStart",
280     "2nd.OnTestStart",
281     "ListenerTest::SetUp",
282     "ListenerTest::* Test Body",
283     "1st.OnTestPartResult",
284     "2nd.OnTestPartResult",
285     "ListenerTest::TearDown",
286     "2nd.OnTestEnd",
287     "1st.OnTestEnd",
288     "ListenerTest::TearDownTestCase",
289     "2nd.OnTestCaseEnd",
290     "1st.OnTestCaseEnd",
291     "1st.OnEnvironmentsTearDownStart",
292     "2nd.OnEnvironmentsTearDownStart",
293     "Environment::TearDown",
294     "2nd.OnEnvironmentsTearDownEnd",
295     "1st.OnEnvironmentsTearDownEnd",
296     "2nd.OnTestIterationEnd(1)",
297     "1st.OnTestIterationEnd(1)",
298     "2nd.OnTestProgramEnd",
299     "1st.OnTestProgramEnd"
300   };
301   VerifyResults(events,
302                 expected_events,
303                 sizeof(expected_events)/sizeof(expected_events[0]));
304
305   // We need to check manually for ad hoc test failures that happen after
306   // RUN_ALL_TESTS finishes.
307   if (UnitTest::GetInstance()->Failed())
308     ret_val = 1;
309
310   return ret_val;
311 }