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