1 // Copyright 2009 Google Inc. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // The Google C++ Testing and Mocking Framework (Google Test)
32 // This file contains tests verifying correctness of data provided via
33 // UnitTest's public methods.
35 #include "gtest/gtest.h"
37 #include <string.h> // For strcmp.
40 using ::testing::InitGoogleTest
;
47 bool operator()(const T
* a
, const T
* b
) {
48 return strcmp(a
->name(), b
->name()) < 0;
52 class UnitTestHelper
{
54 // Returns the array of pointers to all test suites sorted by the test suite
55 // name. The caller is responsible for deleting the array.
56 static TestSuite
const** GetSortedTestSuites() {
57 UnitTest
& unit_test
= *UnitTest::GetInstance();
58 auto const** const test_suites
= new const TestSuite
*[static_cast<size_t>(
59 unit_test
.total_test_suite_count())];
61 for (int i
= 0; i
< unit_test
.total_test_suite_count(); ++i
)
62 test_suites
[i
] = unit_test
.GetTestSuite(i
);
64 std::sort(test_suites
,
65 test_suites
+ unit_test
.total_test_suite_count(),
66 LessByName
<TestSuite
>());
70 // Returns the test suite by its name. The caller doesn't own the returned
72 static const TestSuite
* FindTestSuite(const char* name
) {
73 UnitTest
& unit_test
= *UnitTest::GetInstance();
74 for (int i
= 0; i
< unit_test
.total_test_suite_count(); ++i
) {
75 const TestSuite
* test_suite
= unit_test
.GetTestSuite(i
);
76 if (0 == strcmp(test_suite
->name(), name
))
82 // Returns the array of pointers to all tests in a particular test suite
83 // sorted by the test name. The caller is responsible for deleting the
85 static TestInfo
const** GetSortedTests(const TestSuite
* test_suite
) {
86 TestInfo
const** const tests
= new const TestInfo
*[static_cast<size_t>(
87 test_suite
->total_test_count())];
89 for (int i
= 0; i
< test_suite
->total_test_count(); ++i
)
90 tests
[i
] = test_suite
->GetTestInfo(i
);
92 std::sort(tests
, tests
+ test_suite
->total_test_count(),
93 LessByName
<TestInfo
>());
98 #if GTEST_HAS_TYPED_TEST
99 template <typename T
> class TestSuiteWithCommentTest
: public Test
{};
100 TYPED_TEST_SUITE(TestSuiteWithCommentTest
, Types
<int>);
101 TYPED_TEST(TestSuiteWithCommentTest
, Dummy
) {}
103 const int kTypedTestSuites
= 1;
104 const int kTypedTests
= 1;
106 const int kTypedTestSuites
= 0;
107 const int kTypedTests
= 0;
108 #endif // GTEST_HAS_TYPED_TEST
110 // We can only test the accessors that do not change value while tests run.
111 // Since tests can be run in any order, the values the accessors that track
112 // test execution (such as failed_test_count) can not be predicted.
113 TEST(ApiTest
, UnitTestImmutableAccessorsWork
) {
114 UnitTest
* unit_test
= UnitTest::GetInstance();
116 ASSERT_EQ(2 + kTypedTestSuites
, unit_test
->total_test_suite_count());
117 EXPECT_EQ(1 + kTypedTestSuites
, unit_test
->test_suite_to_run_count());
118 EXPECT_EQ(2, unit_test
->disabled_test_count());
119 EXPECT_EQ(5 + kTypedTests
, unit_test
->total_test_count());
120 EXPECT_EQ(3 + kTypedTests
, unit_test
->test_to_run_count());
122 const TestSuite
** const test_suites
= UnitTestHelper::GetSortedTestSuites();
124 EXPECT_STREQ("ApiTest", test_suites
[0]->name());
125 EXPECT_STREQ("DISABLED_Test", test_suites
[1]->name());
126 #if GTEST_HAS_TYPED_TEST
127 EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites
[2]->name());
128 #endif // GTEST_HAS_TYPED_TEST
130 delete[] test_suites
;
132 // The following lines initiate actions to verify certain methods in
133 // FinalSuccessChecker::TearDown.
135 // Records a test property to verify TestResult::GetTestProperty().
136 RecordProperty("key", "value");
139 AssertionResult
IsNull(const char* str
) {
140 if (str
!= nullptr) {
141 return testing::AssertionFailure() << "argument is " << str
;
143 return AssertionSuccess();
146 TEST(ApiTest
, TestSuiteImmutableAccessorsWork
) {
147 const TestSuite
* test_suite
= UnitTestHelper::FindTestSuite("ApiTest");
148 ASSERT_TRUE(test_suite
!= nullptr);
150 EXPECT_STREQ("ApiTest", test_suite
->name());
151 EXPECT_TRUE(IsNull(test_suite
->type_param()));
152 EXPECT_TRUE(test_suite
->should_run());
153 EXPECT_EQ(1, test_suite
->disabled_test_count());
154 EXPECT_EQ(3, test_suite
->test_to_run_count());
155 ASSERT_EQ(4, test_suite
->total_test_count());
157 const TestInfo
** tests
= UnitTestHelper::GetSortedTests(test_suite
);
159 EXPECT_STREQ("DISABLED_Dummy1", tests
[0]->name());
160 EXPECT_STREQ("ApiTest", tests
[0]->test_suite_name());
161 EXPECT_TRUE(IsNull(tests
[0]->value_param()));
162 EXPECT_TRUE(IsNull(tests
[0]->type_param()));
163 EXPECT_FALSE(tests
[0]->should_run());
165 EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests
[1]->name());
166 EXPECT_STREQ("ApiTest", tests
[1]->test_suite_name());
167 EXPECT_TRUE(IsNull(tests
[1]->value_param()));
168 EXPECT_TRUE(IsNull(tests
[1]->type_param()));
169 EXPECT_TRUE(tests
[1]->should_run());
171 EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests
[2]->name());
172 EXPECT_STREQ("ApiTest", tests
[2]->test_suite_name());
173 EXPECT_TRUE(IsNull(tests
[2]->value_param()));
174 EXPECT_TRUE(IsNull(tests
[2]->type_param()));
175 EXPECT_TRUE(tests
[2]->should_run());
177 EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests
[3]->name());
178 EXPECT_STREQ("ApiTest", tests
[3]->test_suite_name());
179 EXPECT_TRUE(IsNull(tests
[3]->value_param()));
180 EXPECT_TRUE(IsNull(tests
[3]->type_param()));
181 EXPECT_TRUE(tests
[3]->should_run());
186 #if GTEST_HAS_TYPED_TEST
187 test_suite
= UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
188 ASSERT_TRUE(test_suite
!= nullptr);
190 EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite
->name());
191 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(), test_suite
->type_param());
192 EXPECT_TRUE(test_suite
->should_run());
193 EXPECT_EQ(0, test_suite
->disabled_test_count());
194 EXPECT_EQ(1, test_suite
->test_to_run_count());
195 ASSERT_EQ(1, test_suite
->total_test_count());
197 tests
= UnitTestHelper::GetSortedTests(test_suite
);
199 EXPECT_STREQ("Dummy", tests
[0]->name());
200 EXPECT_STREQ("TestSuiteWithCommentTest/0", tests
[0]->test_suite_name());
201 EXPECT_TRUE(IsNull(tests
[0]->value_param()));
202 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(), tests
[0]->type_param());
203 EXPECT_TRUE(tests
[0]->should_run());
206 #endif // GTEST_HAS_TYPED_TEST
209 TEST(ApiTest
, TestSuiteDisabledAccessorsWork
) {
210 const TestSuite
* test_suite
= UnitTestHelper::FindTestSuite("DISABLED_Test");
211 ASSERT_TRUE(test_suite
!= nullptr);
213 EXPECT_STREQ("DISABLED_Test", test_suite
->name());
214 EXPECT_TRUE(IsNull(test_suite
->type_param()));
215 EXPECT_FALSE(test_suite
->should_run());
216 EXPECT_EQ(1, test_suite
->disabled_test_count());
217 EXPECT_EQ(0, test_suite
->test_to_run_count());
218 ASSERT_EQ(1, test_suite
->total_test_count());
220 const TestInfo
* const test_info
= test_suite
->GetTestInfo(0);
221 EXPECT_STREQ("Dummy2", test_info
->name());
222 EXPECT_STREQ("DISABLED_Test", test_info
->test_suite_name());
223 EXPECT_TRUE(IsNull(test_info
->value_param()));
224 EXPECT_TRUE(IsNull(test_info
->type_param()));
225 EXPECT_FALSE(test_info
->should_run());
228 // These two tests are here to provide support for testing
229 // test_suite_to_run_count, disabled_test_count, and test_to_run_count.
230 TEST(ApiTest
, DISABLED_Dummy1
) {}
231 TEST(DISABLED_Test
, Dummy2
) {}
233 class FinalSuccessChecker
: public Environment
{
235 void TearDown() override
{
236 UnitTest
* unit_test
= UnitTest::GetInstance();
238 EXPECT_EQ(1 + kTypedTestSuites
, unit_test
->successful_test_suite_count());
239 EXPECT_EQ(3 + kTypedTests
, unit_test
->successful_test_count());
240 EXPECT_EQ(0, unit_test
->failed_test_suite_count());
241 EXPECT_EQ(0, unit_test
->failed_test_count());
242 EXPECT_TRUE(unit_test
->Passed());
243 EXPECT_FALSE(unit_test
->Failed());
244 ASSERT_EQ(2 + kTypedTestSuites
, unit_test
->total_test_suite_count());
246 const TestSuite
** const test_suites
= UnitTestHelper::GetSortedTestSuites();
248 EXPECT_STREQ("ApiTest", test_suites
[0]->name());
249 EXPECT_TRUE(IsNull(test_suites
[0]->type_param()));
250 EXPECT_TRUE(test_suites
[0]->should_run());
251 EXPECT_EQ(1, test_suites
[0]->disabled_test_count());
252 ASSERT_EQ(4, test_suites
[0]->total_test_count());
253 EXPECT_EQ(3, test_suites
[0]->successful_test_count());
254 EXPECT_EQ(0, test_suites
[0]->failed_test_count());
255 EXPECT_TRUE(test_suites
[0]->Passed());
256 EXPECT_FALSE(test_suites
[0]->Failed());
258 EXPECT_STREQ("DISABLED_Test", test_suites
[1]->name());
259 EXPECT_TRUE(IsNull(test_suites
[1]->type_param()));
260 EXPECT_FALSE(test_suites
[1]->should_run());
261 EXPECT_EQ(1, test_suites
[1]->disabled_test_count());
262 ASSERT_EQ(1, test_suites
[1]->total_test_count());
263 EXPECT_EQ(0, test_suites
[1]->successful_test_count());
264 EXPECT_EQ(0, test_suites
[1]->failed_test_count());
266 #if GTEST_HAS_TYPED_TEST
267 EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites
[2]->name());
268 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(),
269 test_suites
[2]->type_param());
270 EXPECT_TRUE(test_suites
[2]->should_run());
271 EXPECT_EQ(0, test_suites
[2]->disabled_test_count());
272 ASSERT_EQ(1, test_suites
[2]->total_test_count());
273 EXPECT_EQ(1, test_suites
[2]->successful_test_count());
274 EXPECT_EQ(0, test_suites
[2]->failed_test_count());
275 EXPECT_TRUE(test_suites
[2]->Passed());
276 EXPECT_FALSE(test_suites
[2]->Failed());
277 #endif // GTEST_HAS_TYPED_TEST
279 const TestSuite
* test_suite
= UnitTestHelper::FindTestSuite("ApiTest");
280 const TestInfo
** tests
= UnitTestHelper::GetSortedTests(test_suite
);
281 EXPECT_STREQ("DISABLED_Dummy1", tests
[0]->name());
282 EXPECT_STREQ("ApiTest", tests
[0]->test_suite_name());
283 EXPECT_FALSE(tests
[0]->should_run());
285 EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests
[1]->name());
286 EXPECT_STREQ("ApiTest", tests
[1]->test_suite_name());
287 EXPECT_TRUE(IsNull(tests
[1]->value_param()));
288 EXPECT_TRUE(IsNull(tests
[1]->type_param()));
289 EXPECT_TRUE(tests
[1]->should_run());
290 EXPECT_TRUE(tests
[1]->result()->Passed());
291 EXPECT_EQ(0, tests
[1]->result()->test_property_count());
293 EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests
[2]->name());
294 EXPECT_STREQ("ApiTest", tests
[2]->test_suite_name());
295 EXPECT_TRUE(IsNull(tests
[2]->value_param()));
296 EXPECT_TRUE(IsNull(tests
[2]->type_param()));
297 EXPECT_TRUE(tests
[2]->should_run());
298 EXPECT_TRUE(tests
[2]->result()->Passed());
299 EXPECT_EQ(0, tests
[2]->result()->test_property_count());
301 EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests
[3]->name());
302 EXPECT_STREQ("ApiTest", tests
[3]->test_suite_name());
303 EXPECT_TRUE(IsNull(tests
[3]->value_param()));
304 EXPECT_TRUE(IsNull(tests
[3]->type_param()));
305 EXPECT_TRUE(tests
[3]->should_run());
306 EXPECT_TRUE(tests
[3]->result()->Passed());
307 EXPECT_EQ(1, tests
[3]->result()->test_property_count());
308 const TestProperty
& property
= tests
[3]->result()->GetTestProperty(0);
309 EXPECT_STREQ("key", property
.key());
310 EXPECT_STREQ("value", property
.value());
314 #if GTEST_HAS_TYPED_TEST
315 test_suite
= UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
316 tests
= UnitTestHelper::GetSortedTests(test_suite
);
318 EXPECT_STREQ("Dummy", tests
[0]->name());
319 EXPECT_STREQ("TestSuiteWithCommentTest/0", tests
[0]->test_suite_name());
320 EXPECT_TRUE(IsNull(tests
[0]->value_param()));
321 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(), tests
[0]->type_param());
322 EXPECT_TRUE(tests
[0]->should_run());
323 EXPECT_TRUE(tests
[0]->result()->Passed());
324 EXPECT_EQ(0, tests
[0]->result()->test_property_count());
327 #endif // GTEST_HAS_TYPED_TEST
328 delete[] test_suites
;
332 } // namespace internal
333 } // namespace testing
335 int main(int argc
, char **argv
) {
336 InitGoogleTest(&argc
, argv
);
338 AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
340 return RUN_ALL_TESTS();