1
// Copyright 2009 Google Inc. All rights reserved.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are
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
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.
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.
29
// Author: vladl@google.com (Vlad Losev)
31
// The Google C++ Testing Framework (Google Test)
33
// This file verifies Google Test event listeners receive events at the
36
#include <gtest/gtest.h>
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
43
#define GTEST_IMPLEMENTATION_ 1
44
#include "src/gtest-internal-inl.h" // For Vector.
45
#undef GTEST_IMPLEMENTATION_
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;
59
// Used by tests to register their events.
60
Vector<String>* g_events = NULL;
65
class EventRecordingListener : public TestEventListener {
67
EventRecordingListener(const char* name) : name_(name) {}
70
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
71
g_events->PushBack(GetFullMethodName("OnTestProgramStart"));
74
virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
77
message << GetFullMethodName("OnTestIterationStart")
78
<< "(" << iteration << ")";
79
g_events->PushBack(message.GetString());
82
virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
83
g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpStart"));
86
virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
87
g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpEnd"));
90
virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
91
g_events->PushBack(GetFullMethodName("OnTestCaseStart"));
94
virtual void OnTestStart(const TestInfo& /*test_info*/) {
95
g_events->PushBack(GetFullMethodName("OnTestStart"));
98
virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
99
g_events->PushBack(GetFullMethodName("OnTestPartResult"));
102
virtual void OnTestEnd(const TestInfo& /*test_info*/) {
103
g_events->PushBack(GetFullMethodName("OnTestEnd"));
106
virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
107
g_events->PushBack(GetFullMethodName("OnTestCaseEnd"));
110
virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
111
g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownStart"));
114
virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
115
g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownEnd"));
118
virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
121
message << GetFullMethodName("OnTestIterationEnd")
122
<< "(" << iteration << ")";
123
g_events->PushBack(message.GetString());
126
virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
127
g_events->PushBack(GetFullMethodName("OnTestProgramEnd"));
131
String GetFullMethodName(const char* name) {
133
message << name_ << "." << name;
134
return message.GetString();
140
class EnvironmentInvocationCatcher : public Environment {
142
virtual void SetUp() {
143
g_events->PushBack(String("Environment::SetUp"));
146
virtual void TearDown() {
147
g_events->PushBack(String("Environment::TearDown"));
151
class ListenerTest : public Test {
153
static void SetUpTestCase() {
154
g_events->PushBack(String("ListenerTest::SetUpTestCase"));
157
static void TearDownTestCase() {
158
g_events->PushBack(String("ListenerTest::TearDownTestCase"));
161
virtual void SetUp() {
162
g_events->PushBack(String("ListenerTest::SetUp"));
165
virtual void TearDown() {
166
g_events->PushBack(String("ListenerTest::TearDown"));
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.
177
TEST_F(ListenerTest, DoesBar) {
178
g_events->PushBack(String("ListenerTest::* Test Body"));
179
SUCCEED(); // Triggers OnTestPartResult.
182
} // namespace internal
184
} // namespace testing
186
using ::testing::internal::EnvironmentInvocationCatcher;
187
using ::testing::internal::EventRecordingListener;
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);
197
// Compares the common prefix.
198
const int shorter_size = expected_data_size <= actual_size ?
199
expected_data_size : actual_size;
201
for (; i < shorter_size; ++i) {
202
ASSERT_STREQ(expected_data[i], data.GetElement(i).c_str())
203
<< "at position " << i;
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());
212
int main(int argc, char **argv) {
213
Vector<String> events;
215
InitGoogleTest(&argc, argv);
217
UnitTest::GetInstance()->listeners().Append(
218
new EventRecordingListener("1st"));
219
UnitTest::GetInstance()->listeners().Append(
220
new EventRecordingListener("2nd"));
222
AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
224
GTEST_CHECK_(events.size() == 0)
225
<< "AddGlobalTestEnvironment should not generate any events itself.";
227
::testing::GTEST_FLAG(repeat) = 2;
228
int ret_val = RUN_ALL_TESTS();
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",
245
"ListenerTest::SetUp",
246
"ListenerTest::* Test Body",
247
"1st.OnTestPartResult",
248
"2nd.OnTestPartResult",
249
"ListenerTest::TearDown",
254
"ListenerTest::SetUp",
255
"ListenerTest::* Test Body",
256
"1st.OnTestPartResult",
257
"2nd.OnTestPartResult",
258
"ListenerTest::TearDown",
261
"ListenerTest::TearDownTestCase",
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",
283
"ListenerTest::SetUp",
284
"ListenerTest::* Test Body",
285
"1st.OnTestPartResult",
286
"2nd.OnTestPartResult",
287
"ListenerTest::TearDown",
292
"ListenerTest::SetUp",
293
"ListenerTest::* Test Body",
294
"1st.OnTestPartResult",
295
"2nd.OnTestPartResult",
296
"ListenerTest::TearDown",
299
"ListenerTest::TearDownTestCase",
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"
312
VerifyResults(events,
314
sizeof(expected_events)/sizeof(expected_events[0]));
316
// We need to check manually for ad hoc test failures that happen after
317
// RUN_ALL_TESTS finishes.
318
if (UnitTest::GetInstance()->Failed())