1
// Copyright 2007, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
// Author: wan@google.com (Zhanyong Wan)
32
// Tests using global test environments.
36
#include <gtest/gtest.h>
39
GTEST_DECLARE_string_(filter);
45
NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
48
// For testing using global test environments.
49
class MyEnvironment : public testing::Environment {
51
MyEnvironment() { Reset(); }
53
// Depending on the value of failure_in_set_up_, SetUp() will
54
// generate a non-fatal failure, generate a fatal failure, or
56
virtual void SetUp() {
57
set_up_was_run_ = true;
59
switch (failure_in_set_up_) {
60
case NON_FATAL_FAILURE:
61
ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
64
FAIL() << "Expected fatal failure in global set-up.";
71
// Generates a non-fatal failure.
72
virtual void TearDown() {
73
tear_down_was_run_ = true;
74
ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
77
// Resets the state of the environment s.t. it can be reused.
79
failure_in_set_up_ = NO_FAILURE;
80
set_up_was_run_ = false;
81
tear_down_was_run_ = false;
84
// We call this function to set the type of failure SetUp() should
86
void set_failure_in_set_up(FailureType type) {
87
failure_in_set_up_ = type;
91
bool set_up_was_run() const { return set_up_was_run_; }
93
// Was TearDown() run?
94
bool tear_down_was_run() const { return tear_down_was_run_; }
96
FailureType failure_in_set_up_;
98
bool tear_down_was_run_;
104
// The sole purpose of this TEST is to enable us to check whether it
110
// Prints the message and aborts the program if condition is false.
111
void Check(bool condition, const char* msg) {
113
printf("FAILED: %s\n", msg);
118
// Runs the tests. Return true iff successful.
120
// The 'failure' parameter specifies the type of failure that should
121
// be generated by the global set-up.
122
int RunAllTests(MyEnvironment* env, FailureType failure) {
124
env->set_failure_in_set_up(failure);
125
test_was_run = false;
126
return RUN_ALL_TESTS();
131
int main(int argc, char **argv) {
132
testing::InitGoogleTest(&argc, argv);
134
// Registers a global test environment, and verifies that the
135
// registration function returns its argument.
136
MyEnvironment* const env = new MyEnvironment;
137
Check(testing::AddGlobalTestEnvironment(env) == env,
138
"AddGlobalTestEnvironment() should return its argument.");
140
// Verifies that RUN_ALL_TESTS() runs the tests when the global
141
// set-up is successful.
142
Check(RunAllTests(env, NO_FAILURE) != 0,
143
"RUN_ALL_TESTS() should return non-zero, as the global tear-down "
144
"should generate a failure.");
146
"The tests should run, as the global set-up should generate no "
148
Check(env->tear_down_was_run(),
149
"The global tear-down should run, as the global set-up was run.");
151
// Verifies that RUN_ALL_TESTS() runs the tests when the global
152
// set-up generates no fatal failure.
153
Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
154
"RUN_ALL_TESTS() should return non-zero, as both the global set-up "
155
"and the global tear-down should generate a non-fatal failure.");
157
"The tests should run, as the global set-up should generate no "
159
Check(env->tear_down_was_run(),
160
"The global tear-down should run, as the global set-up was run.");
162
// Verifies that RUN_ALL_TESTS() runs no test when the global set-up
163
// generates a fatal failure.
164
Check(RunAllTests(env, FATAL_FAILURE) != 0,
165
"RUN_ALL_TESTS() should return non-zero, as the global set-up "
166
"should generate a fatal failure.");
168
"The tests should not run, as the global set-up should generate "
170
Check(env->tear_down_was_run(),
171
"The global tear-down should run, as the global set-up was run.");
173
// Verifies that RUN_ALL_TESTS() doesn't do global set-up or
174
// tear-down when there is no test to run.
175
testing::GTEST_FLAG(filter) = "-*";
176
Check(RunAllTests(env, NO_FAILURE) == 0,
177
"RUN_ALL_TESTS() should return zero, as there is no test to run.");
178
Check(!env->set_up_was_run(),
179
"The global set-up should not run, as there is no test to run.");
180
Check(!env->tear_down_was_run(),
181
"The global tear-down should not run, "
182
"as the global set-up was not run.");