Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest_environment_test.cc
Go to the documentation of this file.
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 //
31 // Tests using global test environments.
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include "gtest/gtest.h"
36 #include "src/gtest-internal-inl.h"
37 
38 namespace testing {
39 GTEST_DECLARE_string_(filter);
40 }
41 
42 namespace {
43 
45  NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
46 };
47 
48 // For testing using global test environments.
49 class MyEnvironment : public testing::Environment {
50  public:
51  MyEnvironment() { Reset(); }
52 
53  // Depending on the value of failure_in_set_up_, SetUp() will
54  // generate a non-fatal failure, generate a fatal failure, or
55  // succeed.
56  void SetUp() override {
57  set_up_was_run_ = true;
58 
59  switch (failure_in_set_up_) {
60  case NON_FATAL_FAILURE:
61  ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
62  break;
63  case FATAL_FAILURE:
64  FAIL() << "Expected fatal failure in global set-up.";
65  break;
66  default:
67  break;
68  }
69  }
70 
71  // Generates a non-fatal failure.
72  void TearDown() override {
73  tear_down_was_run_ = true;
74  ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
75  }
76 
77  // Resets the state of the environment s.t. it can be reused.
78  void Reset() {
79  failure_in_set_up_ = NO_FAILURE;
80  set_up_was_run_ = false;
81  tear_down_was_run_ = false;
82  }
83 
84  // We call this function to set the type of failure SetUp() should
85  // generate.
86  void set_failure_in_set_up(FailureType type) {
87  failure_in_set_up_ = type;
88  }
89 
90  // Was SetUp() run?
91  bool set_up_was_run() const { return set_up_was_run_; }
92 
93  // Was TearDown() run?
94  bool tear_down_was_run() const { return tear_down_was_run_; }
95 
96  private:
97  FailureType failure_in_set_up_;
98  bool set_up_was_run_;
99  bool tear_down_was_run_;
100 };
101 
102 // Was the TEST run?
103 bool test_was_run;
104 
105 // The sole purpose of this TEST is to enable us to check whether it
106 // was run.
107 TEST(FooTest, Bar) {
108  test_was_run = true;
109 }
110 
111 // Prints the message and aborts the program if condition is false.
112 void Check(bool condition, const char* msg) {
113  if (!condition) {
114  printf("FAILED: %s\n", msg);
116  }
117 }
118 
119 // Runs the tests. Return true if and only if successful.
120 //
121 // The 'failure' parameter specifies the type of failure that should
122 // be generated by the global set-up.
123 int RunAllTests(MyEnvironment* env, FailureType failure) {
124  env->Reset();
125  env->set_failure_in_set_up(failure);
126  test_was_run = false;
127  testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
128  return RUN_ALL_TESTS();
129 }
130 
131 } // namespace
132 
133 int main(int argc, char **argv) {
134  testing::InitGoogleTest(&argc, argv);
135 
136  // Registers a global test environment, and verifies that the
137  // registration function returns its argument.
138  MyEnvironment* const env = new MyEnvironment;
139  Check(testing::AddGlobalTestEnvironment(env) == env,
140  "AddGlobalTestEnvironment() should return its argument.");
141 
142  // Verifies that RUN_ALL_TESTS() runs the tests when the global
143  // set-up is successful.
144  Check(RunAllTests(env, NO_FAILURE) != 0,
145  "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
146  "should generate a failure.");
147  Check(test_was_run,
148  "The tests should run, as the global set-up should generate no "
149  "failure");
150  Check(env->tear_down_was_run(),
151  "The global tear-down should run, as the global set-up was run.");
152 
153  // Verifies that RUN_ALL_TESTS() runs the tests when the global
154  // set-up generates no fatal failure.
155  Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
156  "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
157  "and the global tear-down should generate a non-fatal failure.");
158  Check(test_was_run,
159  "The tests should run, as the global set-up should generate no "
160  "fatal failure.");
161  Check(env->tear_down_was_run(),
162  "The global tear-down should run, as the global set-up was run.");
163 
164  // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
165  // generates a fatal failure.
166  Check(RunAllTests(env, FATAL_FAILURE) != 0,
167  "RUN_ALL_TESTS() should return non-zero, as the global set-up "
168  "should generate a fatal failure.");
169  Check(!test_was_run,
170  "The tests should not run, as the global set-up should generate "
171  "a fatal failure.");
172  Check(env->tear_down_was_run(),
173  "The global tear-down should run, as the global set-up was run.");
174 
175  // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
176  // tear-down when there is no test to run.
177  testing::GTEST_FLAG(filter) = "-*";
178  Check(RunAllTests(env, NO_FAILURE) == 0,
179  "RUN_ALL_TESTS() should return zero, as there is no test to run.");
180  Check(!env->set_up_was_run(),
181  "The global set-up should not run, as there is no test to run.");
182  Check(!env->tear_down_was_run(),
183  "The global tear-down should not run, "
184  "as the global set-up was not run.");
185 
186  printf("PASS\n");
187  return 0;
188 }
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1492
virtual void SetUp()
Definition: gtest.h:1067
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2348
#define GTEST_FLAG(name)
Definition: gtest-port.h:2187
GTEST_DECLARE_string_(death_test_style)
int main()
Definition: ad_example.cpp:191
int RunAllTests()
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2484
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6527
#define ADD_FAILURE()
Definition: gtest.h:1923
#define FAIL()
Definition: gtest.h:1942
virtual void TearDown()
Definition: gtest.h:1070