OLD | NEW |
1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 bool set_up_was_run() const { return set_up_was_run_; } | 95 bool set_up_was_run() const { return set_up_was_run_; } |
96 | 96 |
97 // Was TearDown() run? | 97 // Was TearDown() run? |
98 bool tear_down_was_run() const { return tear_down_was_run_; } | 98 bool tear_down_was_run() const { return tear_down_was_run_; } |
99 private: | 99 private: |
100 FailureType failure_in_set_up_; | 100 FailureType failure_in_set_up_; |
101 bool set_up_was_run_; | 101 bool set_up_was_run_; |
102 bool tear_down_was_run_; | 102 bool tear_down_was_run_; |
103 }; | 103 }; |
104 | 104 |
105 // Was the TEST run? | |
106 bool test_was_run; | |
107 | |
108 // The sole purpose of this TEST is to enable us to check whether it | 105 // The sole purpose of this TEST is to enable us to check whether it |
109 // was run. | 106 // was run. |
110 TEST(FooTest, Bar) { | 107 TEST(FooTest, Bar) { |
111 test_was_run = true; | |
112 } | 108 } |
113 | 109 |
114 // Prints the message and aborts the program if condition is false. | 110 // Prints the message and aborts the program if condition is false. |
115 void Check(bool condition, const char* msg) { | 111 void Check(bool condition, const char* msg) { |
116 if (!condition) { | 112 if (!condition) { |
117 printf("FAILED: %s\n", msg); | 113 printf("FAILED: %s\n", msg); |
118 testing::internal::posix::Abort(); | 114 testing::internal::posix::Abort(); |
119 } | 115 } |
120 } | 116 } |
121 | 117 |
122 // Runs the tests. Return true iff successful. | 118 // Runs the tests. Return true iff successful. |
123 // | 119 // |
124 // The 'failure' parameter specifies the type of failure that should | 120 // The 'failure' parameter specifies the type of failure that should |
125 // be generated by the global set-up. | 121 // be generated by the global set-up. |
126 int RunAllTests(MyEnvironment* env, FailureType failure) { | 122 int RunAllTests(MyEnvironment* env, FailureType failure) { |
127 env->Reset(); | 123 env->Reset(); |
128 env->set_failure_in_set_up(failure); | 124 env->set_failure_in_set_up(failure); |
129 test_was_run = false; | |
130 testing::internal::GetUnitTestImpl()->ClearAdHocTestResult(); | 125 testing::internal::GetUnitTestImpl()->ClearAdHocTestResult(); |
131 return RUN_ALL_TESTS(); | 126 return RUN_ALL_TESTS(); |
132 } | 127 } |
133 | 128 |
134 } // namespace | 129 } // namespace |
135 | 130 |
136 int main(int argc, char **argv) { | 131 int main(int argc, char **argv) { |
137 testing::InitGoogleTest(&argc, argv); | 132 testing::InitGoogleTest(&argc, argv); |
138 | 133 |
139 // Registers a global test environment, and verifies that the | 134 // Registers a global test environment, and verifies that the |
140 // registration function returns its argument. | 135 // registration function returns its argument. |
141 MyEnvironment* const env = new MyEnvironment; | 136 MyEnvironment* const env = new MyEnvironment; |
142 Check(testing::AddGlobalTestEnvironment(env) == env, | 137 Check(testing::AddGlobalTestEnvironment(env) == env, |
143 "AddGlobalTestEnvironment() should return its argument."); | 138 "AddGlobalTestEnvironment() should return its argument."); |
144 | 139 |
145 // Verifies that RUN_ALL_TESTS() runs the tests when the global | 140 // Verifies that RUN_ALL_TESTS() runs the tests when the global |
146 // set-up is successful. | 141 // set-up is successful. |
147 Check(RunAllTests(env, NO_FAILURE) != 0, | 142 Check(RunAllTests(env, NO_FAILURE) != 0, |
148 "RUN_ALL_TESTS() should return non-zero, as the global tear-down " | 143 "RUN_ALL_TESTS() should return non-zero, as the global tear-down " |
149 "should generate a failure."); | 144 "should generate a failure."); |
150 Check(test_was_run, | 145 |
| 146 Check(testing::UnitTest::GetInstance()->successful_test_case_count(), |
151 "The tests should run, as the global set-up should generate no " | 147 "The tests should run, as the global set-up should generate no " |
152 "failure"); | 148 "failure"); |
153 Check(env->tear_down_was_run(), | 149 Check(env->tear_down_was_run(), |
154 "The global tear-down should run, as the global set-up was run."); | 150 "The global tear-down should run, as the global set-up was run."); |
155 | 151 |
156 // Verifies that RUN_ALL_TESTS() runs the tests when the global | 152 // Verifies that RUN_ALL_TESTS() runs the tests when the global |
157 // set-up generates no fatal failure. | 153 // set-up generates no fatal failure. |
158 Check(RunAllTests(env, NON_FATAL_FAILURE) != 0, | 154 Check(RunAllTests(env, NON_FATAL_FAILURE) != 0, |
159 "RUN_ALL_TESTS() should return non-zero, as both the global set-up " | 155 "RUN_ALL_TESTS() should return non-zero, as both the global set-up " |
160 "and the global tear-down should generate a non-fatal failure."); | 156 "and the global tear-down should generate a non-fatal failure."); |
161 Check(test_was_run, | 157 Check(testing::UnitTest::GetInstance()->successful_test_case_count(), |
162 "The tests should run, as the global set-up should generate no " | 158 "The tests should run, as the global set-up should generate no " |
163 "fatal failure."); | 159 "failure"); |
164 Check(env->tear_down_was_run(), | 160 Check(env->tear_down_was_run(), |
165 "The global tear-down should run, as the global set-up was run."); | 161 "The global tear-down should run, as the global set-up was run."); |
166 | 162 |
167 // Verifies that RUN_ALL_TESTS() runs no test when the global set-up | 163 // Verifies that RUN_ALL_TESTS() runs no test when the global set-up |
168 // generates a fatal failure. | 164 // generates a fatal failure. |
169 Check(RunAllTests(env, FATAL_FAILURE) != 0, | 165 Check(RunAllTests(env, FATAL_FAILURE) != 0, |
170 "RUN_ALL_TESTS() should return non-zero, as the global set-up " | 166 "RUN_ALL_TESTS() should return non-zero, as the global set-up " |
171 "should generate a fatal failure."); | 167 "should generate a fatal failure."); |
172 Check(!test_was_run, | 168 Check(testing::UnitTest::GetInstance()->successful_test_case_count(), |
173 "The tests should not run, as the global set-up should generate " | 169 "The tests should run, as the global set-up should generate no " |
174 "a fatal failure."); | 170 "failure"); |
175 Check(env->tear_down_was_run(), | 171 Check(env->tear_down_was_run(), |
176 "The global tear-down should run, as the global set-up was run."); | 172 "The global tear-down should run, as the global set-up was run."); |
177 | 173 |
178 // Verifies that RUN_ALL_TESTS() doesn't do global set-up or | 174 // Verifies that RUN_ALL_TESTS() doesn't do global set-up or |
179 // tear-down when there is no test to run. | 175 // tear-down when there is no test to run. |
180 testing::GTEST_FLAG(filter) = "-*"; | 176 testing::GTEST_FLAG(filter) = "-*"; |
181 Check(RunAllTests(env, NO_FAILURE) == 0, | 177 Check(RunAllTests(env, NO_FAILURE) == 0, |
182 "RUN_ALL_TESTS() should return zero, as there is no test to run."); | 178 "RUN_ALL_TESTS() should return zero, as there is no test to run."); |
183 Check(!env->set_up_was_run(), | 179 Check(!env->set_up_was_run(), |
184 "The global set-up should not run, as there is no test to run."); | 180 "The global set-up should not run, as there is no test to run."); |
185 Check(!env->tear_down_was_run(), | 181 Check(!env->tear_down_was_run(), |
186 "The global tear-down should not run, " | 182 "The global tear-down should not run, " |
187 "as the global set-up was not run."); | 183 "as the global set-up was not run."); |
188 | 184 |
189 printf("PASS\n"); | 185 printf("PASS\n"); |
190 return 0; | 186 return 0; |
191 } | 187 } |
OLD | NEW |