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 template <typename T
> class TestSuiteWithCommentTest
: public Test
{};
99 TYPED_TEST_SUITE(TestSuiteWithCommentTest
, Types
<int>);
100 TYPED_TEST(TestSuiteWithCommentTest
, Dummy
) {}
102 const int kTypedTestSuites
= 1;
103 const int kTypedTests
= 1;
105 // We can only test the accessors that do not change value while tests run.
106 // Since tests can be run in any order, the values the accessors that track
107 // test execution (such as failed_test_count) can not be predicted.
108 TEST(ApiTest
, UnitTestImmutableAccessorsWork
) {
109 UnitTest
* unit_test
= UnitTest::GetInstance();
111 ASSERT_EQ(2 + kTypedTestSuites
, unit_test
->total_test_suite_count());
112 EXPECT_EQ(1 + kTypedTestSuites
, unit_test
->test_suite_to_run_count());
113 EXPECT_EQ(2, unit_test
->disabled_test_count());
114 EXPECT_EQ(5 + kTypedTests
, unit_test
->total_test_count());
115 EXPECT_EQ(3 + kTypedTests
, unit_test
->test_to_run_count());
117 const TestSuite
** const test_suites
= UnitTestHelper::GetSortedTestSuites();
119 EXPECT_STREQ("ApiTest", test_suites
[0]->name());
120 EXPECT_STREQ("DISABLED_Test", test_suites
[1]->name());
121 EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites
[2]->name());
123 delete[] test_suites
;
125 // The following lines initiate actions to verify certain methods in
126 // FinalSuccessChecker::TearDown.
128 // Records a test property to verify TestResult::GetTestProperty().
129 RecordProperty("key", "value");
132 AssertionResult
IsNull(const char* str
) {
133 if (str
!= nullptr) {
134 return testing::AssertionFailure() << "argument is " << str
;
136 return AssertionSuccess();
139 TEST(ApiTest
, TestSuiteImmutableAccessorsWork
) {
140 const TestSuite
* test_suite
= UnitTestHelper::FindTestSuite("ApiTest");
141 ASSERT_TRUE(test_suite
!= nullptr);
143 EXPECT_STREQ("ApiTest", test_suite
->name());
144 EXPECT_TRUE(IsNull(test_suite
->type_param()));
145 EXPECT_TRUE(test_suite
->should_run());
146 EXPECT_EQ(1, test_suite
->disabled_test_count());
147 EXPECT_EQ(3, test_suite
->test_to_run_count());
148 ASSERT_EQ(4, test_suite
->total_test_count());
150 const TestInfo
** tests
= UnitTestHelper::GetSortedTests(test_suite
);
152 EXPECT_STREQ("DISABLED_Dummy1", tests
[0]->name());
153 EXPECT_STREQ("ApiTest", tests
[0]->test_suite_name());
154 EXPECT_TRUE(IsNull(tests
[0]->value_param()));
155 EXPECT_TRUE(IsNull(tests
[0]->type_param()));
156 EXPECT_FALSE(tests
[0]->should_run());
158 EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests
[1]->name());
159 EXPECT_STREQ("ApiTest", tests
[1]->test_suite_name());
160 EXPECT_TRUE(IsNull(tests
[1]->value_param()));
161 EXPECT_TRUE(IsNull(tests
[1]->type_param()));
162 EXPECT_TRUE(tests
[1]->should_run());
164 EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests
[2]->name());
165 EXPECT_STREQ("ApiTest", tests
[2]->test_suite_name());
166 EXPECT_TRUE(IsNull(tests
[2]->value_param()));
167 EXPECT_TRUE(IsNull(tests
[2]->type_param()));
168 EXPECT_TRUE(tests
[2]->should_run());
170 EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests
[3]->name());
171 EXPECT_STREQ("ApiTest", tests
[3]->test_suite_name());
172 EXPECT_TRUE(IsNull(tests
[3]->value_param()));
173 EXPECT_TRUE(IsNull(tests
[3]->type_param()));
174 EXPECT_TRUE(tests
[3]->should_run());
179 test_suite
= UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
180 ASSERT_TRUE(test_suite
!= nullptr);
182 EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite
->name());
183 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(), test_suite
->type_param());
184 EXPECT_TRUE(test_suite
->should_run());
185 EXPECT_EQ(0, test_suite
->disabled_test_count());
186 EXPECT_EQ(1, test_suite
->test_to_run_count());
187 ASSERT_EQ(1, test_suite
->total_test_count());
189 tests
= UnitTestHelper::GetSortedTests(test_suite
);
191 EXPECT_STREQ("Dummy", tests
[0]->name());
192 EXPECT_STREQ("TestSuiteWithCommentTest/0", tests
[0]->test_suite_name());
193 EXPECT_TRUE(IsNull(tests
[0]->value_param()));
194 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(), tests
[0]->type_param());
195 EXPECT_TRUE(tests
[0]->should_run());
200 TEST(ApiTest
, TestSuiteDisabledAccessorsWork
) {
201 const TestSuite
* test_suite
= UnitTestHelper::FindTestSuite("DISABLED_Test");
202 ASSERT_TRUE(test_suite
!= nullptr);
204 EXPECT_STREQ("DISABLED_Test", test_suite
->name());
205 EXPECT_TRUE(IsNull(test_suite
->type_param()));
206 EXPECT_FALSE(test_suite
->should_run());
207 EXPECT_EQ(1, test_suite
->disabled_test_count());
208 EXPECT_EQ(0, test_suite
->test_to_run_count());
209 ASSERT_EQ(1, test_suite
->total_test_count());
211 const TestInfo
* const test_info
= test_suite
->GetTestInfo(0);
212 EXPECT_STREQ("Dummy2", test_info
->name());
213 EXPECT_STREQ("DISABLED_Test", test_info
->test_suite_name());
214 EXPECT_TRUE(IsNull(test_info
->value_param()));
215 EXPECT_TRUE(IsNull(test_info
->type_param()));
216 EXPECT_FALSE(test_info
->should_run());
219 // These two tests are here to provide support for testing
220 // test_suite_to_run_count, disabled_test_count, and test_to_run_count.
221 TEST(ApiTest
, DISABLED_Dummy1
) {}
222 TEST(DISABLED_Test
, Dummy2
) {}
224 class FinalSuccessChecker
: public Environment
{
226 void TearDown() override
{
227 UnitTest
* unit_test
= UnitTest::GetInstance();
229 EXPECT_EQ(1 + kTypedTestSuites
, unit_test
->successful_test_suite_count());
230 EXPECT_EQ(3 + kTypedTests
, unit_test
->successful_test_count());
231 EXPECT_EQ(0, unit_test
->failed_test_suite_count());
232 EXPECT_EQ(0, unit_test
->failed_test_count());
233 EXPECT_TRUE(unit_test
->Passed());
234 EXPECT_FALSE(unit_test
->Failed());
235 ASSERT_EQ(2 + kTypedTestSuites
, unit_test
->total_test_suite_count());
237 const TestSuite
** const test_suites
= UnitTestHelper::GetSortedTestSuites();
239 EXPECT_STREQ("ApiTest", test_suites
[0]->name());
240 EXPECT_TRUE(IsNull(test_suites
[0]->type_param()));
241 EXPECT_TRUE(test_suites
[0]->should_run());
242 EXPECT_EQ(1, test_suites
[0]->disabled_test_count());
243 ASSERT_EQ(4, test_suites
[0]->total_test_count());
244 EXPECT_EQ(3, test_suites
[0]->successful_test_count());
245 EXPECT_EQ(0, test_suites
[0]->failed_test_count());
246 EXPECT_TRUE(test_suites
[0]->Passed());
247 EXPECT_FALSE(test_suites
[0]->Failed());
249 EXPECT_STREQ("DISABLED_Test", test_suites
[1]->name());
250 EXPECT_TRUE(IsNull(test_suites
[1]->type_param()));
251 EXPECT_FALSE(test_suites
[1]->should_run());
252 EXPECT_EQ(1, test_suites
[1]->disabled_test_count());
253 ASSERT_EQ(1, test_suites
[1]->total_test_count());
254 EXPECT_EQ(0, test_suites
[1]->successful_test_count());
255 EXPECT_EQ(0, test_suites
[1]->failed_test_count());
257 EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites
[2]->name());
258 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(),
259 test_suites
[2]->type_param());
260 EXPECT_TRUE(test_suites
[2]->should_run());
261 EXPECT_EQ(0, test_suites
[2]->disabled_test_count());
262 ASSERT_EQ(1, test_suites
[2]->total_test_count());
263 EXPECT_EQ(1, test_suites
[2]->successful_test_count());
264 EXPECT_EQ(0, test_suites
[2]->failed_test_count());
265 EXPECT_TRUE(test_suites
[2]->Passed());
266 EXPECT_FALSE(test_suites
[2]->Failed());
268 const TestSuite
* test_suite
= UnitTestHelper::FindTestSuite("ApiTest");
269 const TestInfo
** tests
= UnitTestHelper::GetSortedTests(test_suite
);
270 EXPECT_STREQ("DISABLED_Dummy1", tests
[0]->name());
271 EXPECT_STREQ("ApiTest", tests
[0]->test_suite_name());
272 EXPECT_FALSE(tests
[0]->should_run());
274 EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests
[1]->name());
275 EXPECT_STREQ("ApiTest", tests
[1]->test_suite_name());
276 EXPECT_TRUE(IsNull(tests
[1]->value_param()));
277 EXPECT_TRUE(IsNull(tests
[1]->type_param()));
278 EXPECT_TRUE(tests
[1]->should_run());
279 EXPECT_TRUE(tests
[1]->result()->Passed());
280 EXPECT_EQ(0, tests
[1]->result()->test_property_count());
282 EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests
[2]->name());
283 EXPECT_STREQ("ApiTest", tests
[2]->test_suite_name());
284 EXPECT_TRUE(IsNull(tests
[2]->value_param()));
285 EXPECT_TRUE(IsNull(tests
[2]->type_param()));
286 EXPECT_TRUE(tests
[2]->should_run());
287 EXPECT_TRUE(tests
[2]->result()->Passed());
288 EXPECT_EQ(0, tests
[2]->result()->test_property_count());
290 EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests
[3]->name());
291 EXPECT_STREQ("ApiTest", tests
[3]->test_suite_name());
292 EXPECT_TRUE(IsNull(tests
[3]->value_param()));
293 EXPECT_TRUE(IsNull(tests
[3]->type_param()));
294 EXPECT_TRUE(tests
[3]->should_run());
295 EXPECT_TRUE(tests
[3]->result()->Passed());
296 EXPECT_EQ(1, tests
[3]->result()->test_property_count());
297 const TestProperty
& property
= tests
[3]->result()->GetTestProperty(0);
298 EXPECT_STREQ("key", property
.key());
299 EXPECT_STREQ("value", property
.value());
303 test_suite
= UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
304 tests
= UnitTestHelper::GetSortedTests(test_suite
);
306 EXPECT_STREQ("Dummy", tests
[0]->name());
307 EXPECT_STREQ("TestSuiteWithCommentTest/0", tests
[0]->test_suite_name());
308 EXPECT_TRUE(IsNull(tests
[0]->value_param()));
309 EXPECT_STREQ(GetTypeName
<Types
<int>>().c_str(), tests
[0]->type_param());
310 EXPECT_TRUE(tests
[0]->should_run());
311 EXPECT_TRUE(tests
[0]->result()->Passed());
312 EXPECT_EQ(0, tests
[0]->result()->test_property_count());
315 delete[] test_suites
;
319 } // namespace internal
320 } // namespace testing
322 int main(int argc
, char **argv
) {
323 InitGoogleTest(&argc
, argv
);
325 AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
327 return RUN_ALL_TESTS();