Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include "gtest/gtest.h"
00037
00038 #define GTEST_IMPLEMENTATION_ 1 // Required for the next #include.
00039 #include "src/gtest-internal-inl.h"
00040 #undef GTEST_IMPLEMENTATION_
00041
00042 namespace testing {
00043 GTEST_DECLARE_string_(filter);
00044 }
00045
00046 namespace {
00047
00048 enum FailureType {
00049 NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
00050 };
00051
00052
00053 class MyEnvironment : public testing::Environment {
00054 public:
00055 MyEnvironment() { Reset(); }
00056
00057
00058
00059
00060 virtual void SetUp() {
00061 set_up_was_run_ = true;
00062
00063 switch (failure_in_set_up_) {
00064 case NON_FATAL_FAILURE:
00065 ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
00066 break;
00067 case FATAL_FAILURE:
00068 FAIL() << "Expected fatal failure in global set-up.";
00069 break;
00070 default:
00071 break;
00072 }
00073 }
00074
00075
00076 virtual void TearDown() {
00077 tear_down_was_run_ = true;
00078 ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
00079 }
00080
00081
00082 void Reset() {
00083 failure_in_set_up_ = NO_FAILURE;
00084 set_up_was_run_ = false;
00085 tear_down_was_run_ = false;
00086 }
00087
00088
00089
00090 void set_failure_in_set_up(FailureType type) {
00091 failure_in_set_up_ = type;
00092 }
00093
00094
00095 bool set_up_was_run() const { return set_up_was_run_; }
00096
00097
00098 bool tear_down_was_run() const { return tear_down_was_run_; }
00099
00100 private:
00101 FailureType failure_in_set_up_;
00102 bool set_up_was_run_;
00103 bool tear_down_was_run_;
00104 };
00105
00106
00107 bool test_was_run;
00108
00109
00110
00111 TEST(FooTest, Bar) {
00112 test_was_run = true;
00113 }
00114
00115
00116 void Check(bool condition, const char* msg) {
00117 if (!condition) {
00118 printf("FAILED: %s\n", msg);
00119 testing::internal::posix::Abort();
00120 }
00121 }
00122
00123
00124
00125
00126
00127 int RunAllTests(MyEnvironment* env, FailureType failure) {
00128 env->Reset();
00129 env->set_failure_in_set_up(failure);
00130 test_was_run = false;
00131 testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
00132 return RUN_ALL_TESTS();
00133 }
00134
00135 }
00136
00137 int main(int argc, char **argv) {
00138 testing::InitGoogleTest(&argc, argv);
00139
00140
00141
00142 MyEnvironment* const env = new MyEnvironment;
00143 Check(testing::AddGlobalTestEnvironment(env) == env,
00144 "AddGlobalTestEnvironment() should return its argument.");
00145
00146
00147
00148 Check(RunAllTests(env, NO_FAILURE) != 0,
00149 "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
00150 "should generate a failure.");
00151 Check(test_was_run,
00152 "The tests should run, as the global set-up should generate no "
00153 "failure");
00154 Check(env->tear_down_was_run(),
00155 "The global tear-down should run, as the global set-up was run.");
00156
00157
00158
00159 Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
00160 "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
00161 "and the global tear-down should generate a non-fatal failure.");
00162 Check(test_was_run,
00163 "The tests should run, as the global set-up should generate no "
00164 "fatal failure.");
00165 Check(env->tear_down_was_run(),
00166 "The global tear-down should run, as the global set-up was run.");
00167
00168
00169
00170 Check(RunAllTests(env, FATAL_FAILURE) != 0,
00171 "RUN_ALL_TESTS() should return non-zero, as the global set-up "
00172 "should generate a fatal failure.");
00173 Check(!test_was_run,
00174 "The tests should not run, as the global set-up should generate "
00175 "a fatal failure.");
00176 Check(env->tear_down_was_run(),
00177 "The global tear-down should run, as the global set-up was run.");
00178
00179
00180
00181 testing::GTEST_FLAG(filter) = "-*";
00182 Check(RunAllTests(env, NO_FAILURE) == 0,
00183 "RUN_ALL_TESTS() should return zero, as there is no test to run.");
00184 Check(!env->set_up_was_run(),
00185 "The global set-up should not run, as there is no test to run.");
00186 Check(!env->tear_down_was_run(),
00187 "The global tear-down should not run, "
00188 "as the global set-up was not run.");
00189
00190 printf("PASS\n");
00191 return 0;
00192 }