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