1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31 //
32 // Tests using global test environments.
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include "gtest/gtest.h"
37 
38 #define GTEST_IMPLEMENTATION_ 1  // Required for the next #include.
39 #include "src/gtest-internal-inl.h"
40 #undef GTEST_IMPLEMENTATION_
41 
42 namespace testing {
43 GTEST_DECLARE_string_(filter);
44 }
45 
46 namespace {
47 
48 enum FailureType {
49   NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
50 };
51 
52 // For testing using global test environments.
53 class MyEnvironment : public testing::Environment {
54  public:
MyEnvironment()55   MyEnvironment() { Reset(); }
56 
57   // Depending on the value of failure_in_set_up_, SetUp() will
58   // generate a non-fatal failure, generate a fatal failure, or
59   // succeed.
SetUp()60   virtual void SetUp() {
61     set_up_was_run_ = true;
62 
63     switch (failure_in_set_up_) {
64       case NON_FATAL_FAILURE:
65         ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
66         break;
67       case FATAL_FAILURE:
68         FAIL() << "Expected fatal failure in global set-up.";
69         break;
70       default:
71         break;
72     }
73   }
74 
75   // Generates a non-fatal failure.
TearDown()76   virtual void TearDown() {
77     tear_down_was_run_ = true;
78     ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
79   }
80 
81   // Resets the state of the environment s.t. it can be reused.
Reset()82   void Reset() {
83     failure_in_set_up_ = NO_FAILURE;
84     set_up_was_run_ = false;
85     tear_down_was_run_ = false;
86   }
87 
88   // We call this function to set the type of failure SetUp() should
89   // generate.
set_failure_in_set_up(FailureType type)90   void set_failure_in_set_up(FailureType type) {
91     failure_in_set_up_ = type;
92   }
93 
94   // Was SetUp() run?
set_up_was_run() const95   bool set_up_was_run() const { return set_up_was_run_; }
96 
97   // Was TearDown() run?
tear_down_was_run() const98   bool tear_down_was_run() const { return tear_down_was_run_; }
99 
100  private:
101   FailureType failure_in_set_up_;
102   bool set_up_was_run_;
103   bool tear_down_was_run_;
104 };
105 
106 // Was the TEST run?
107 bool test_was_run;
108 
109 // The sole purpose of this TEST is to enable us to check whether it
110 // was run.
TEST(FooTest,Bar)111 TEST(FooTest, Bar) {
112   test_was_run = true;
113 }
114 
115 // Prints the message and aborts the program if condition is false.
Check(bool condition,const char * msg)116 void Check(bool condition, const char* msg) {
117   if (!condition) {
118     printf("FAILED: %s\n", msg);
119     testing::internal::posix::Abort();
120   }
121 }
122 
123 // Runs the tests.  Return true iff successful.
124 //
125 // The 'failure' parameter specifies the type of failure that should
126 // be generated by the global set-up.
RunAllTests(MyEnvironment * env,FailureType failure)127 int RunAllTests(MyEnvironment* env, FailureType failure) {
128   env->Reset();
129   env->set_failure_in_set_up(failure);
130   test_was_run = false;
131   testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
132   return RUN_ALL_TESTS();
133 }
134 
135 }  // namespace
136 
main(int argc,char ** argv)137 int main(int argc, char **argv) {
138   testing::InitGoogleTest(&argc, argv);
139 
140   // Registers a global test environment, and verifies that the
141   // registration function returns its argument.
142   MyEnvironment* const env = new MyEnvironment;
143   Check(testing::AddGlobalTestEnvironment(env) == env,
144         "AddGlobalTestEnvironment() should return its argument.");
145 
146   // Verifies that RUN_ALL_TESTS() runs the tests when the global
147   // set-up is successful.
148   Check(RunAllTests(env, NO_FAILURE) != 0,
149         "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
150         "should generate a failure.");
151   Check(test_was_run,
152         "The tests should run, as the global set-up should generate no "
153         "failure");
154   Check(env->tear_down_was_run(),
155         "The global tear-down should run, as the global set-up was run.");
156 
157   // Verifies that RUN_ALL_TESTS() runs the tests when the global
158   // set-up generates no fatal failure.
159   Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
160         "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
161         "and the global tear-down should generate a non-fatal failure.");
162   Check(test_was_run,
163         "The tests should run, as the global set-up should generate no "
164         "fatal failure.");
165   Check(env->tear_down_was_run(),
166         "The global tear-down should run, as the global set-up was run.");
167 
168   // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
169   // generates a fatal failure.
170   Check(RunAllTests(env, FATAL_FAILURE) != 0,
171         "RUN_ALL_TESTS() should return non-zero, as the global set-up "
172         "should generate a fatal failure.");
173   Check(!test_was_run,
174         "The tests should not run, as the global set-up should generate "
175         "a fatal failure.");
176   Check(env->tear_down_was_run(),
177         "The global tear-down should run, as the global set-up was run.");
178 
179   // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
180   // tear-down when there is no test to run.
181   testing::GTEST_FLAG(filter) = "-*";
182   Check(RunAllTests(env, NO_FAILURE) == 0,
183         "RUN_ALL_TESTS() should return zero, as there is no test to run.");
184   Check(!env->set_up_was_run(),
185         "The global set-up should not run, as there is no test to run.");
186   Check(!env->tear_down_was_run(),
187         "The global tear-down should not run, "
188         "as the global set-up was not run.");
189 
190   printf("PASS\n");
191   return 0;
192 }
193