| 1 |
// Copyright 2009 Google Inc. All rights reserved. |
| 2 |
// |
| 3 |
// Redistribution and use in source and binary forms, with or without |
| 4 |
// modification, are permitted provided that the following conditions are |
| 5 |
// met: |
| 6 |
// |
| 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 |
| 12 |
// distribution. |
| 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. |
| 16 |
// |
| 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. |
| 28 |
|
| 29 |
// |
| 30 |
// The Google C++ Testing and Mocking Framework (Google Test) |
| 31 |
// |
| 32 |
// This file contains tests verifying correctness of data provided via |
| 33 |
// UnitTest's public methods. |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
|
| 37 |
#include <string.h> // For strcmp. |
| 38 |
#include <algorithm> |
| 39 |
|
| 40 |
using ::testing::InitGoogleTest; |
| 41 |
|
| 42 |
namespace testing { |
| 43 |
namespace internal { |
| 44 |
|
| 45 |
template <typename T> |
| 46 |
struct LessByName { |
| 47 |
bool operator()(const T* a, const T* b) { |
| 48 |
return strcmp(a->name(), b->name()) < 0; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
class UnitTestHelper { |
| 53 |
public: |
| 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())]; |
| 60 |
|
| 61 |
for (int i = 0; i < unit_test.total_test_suite_count(); ++i) |
| 62 |
test_suites[i] = unit_test.GetTestSuite(i); |
| 63 |
|
| 64 |
std::sort(test_suites, |
| 65 |
test_suites + unit_test.total_test_suite_count(), |
| 66 |
LessByName<TestSuite>()); |
| 67 |
return test_suites; |
| 68 |
} |
| 69 |
|
| 70 |
// Returns the test suite by its name. The caller doesn't own the returned |
| 71 |
// pointer. |
| 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)) |
| 77 |
return test_suite; |
| 78 |
} |
| 79 |
return nullptr; |
| 80 |
} |
| 81 |
|
| 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 |
| 84 |
// array. |
| 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())]; |
| 88 |
|
| 89 |
for (int i = 0; i < test_suite->total_test_count(); ++i) |
| 90 |
tests[i] = test_suite->GetTestInfo(i); |
| 91 |
|
| 92 |
std::sort(tests, tests + test_suite->total_test_count(), |
| 93 |
LessByName<TestInfo>()); |
| 94 |
return tests; |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
template <typename T> class TestSuiteWithCommentTest : public Test {}; |
| 99 |
TYPED_TEST_SUITE(TestSuiteWithCommentTest, Types<int>); |
| 100 |
TYPED_TEST(TestSuiteWithCommentTest, Dummy) {} |
| 101 |
|
| 102 |
const int kTypedTestSuites = 1; |
| 103 |
const int kTypedTests = 1; |
| 104 |
|
| 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(); |
| 110 |
|
| 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()); |
| 116 |
|
| 117 |
const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites(); |
| 118 |
|
| 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()); |
| 122 |
|
| 123 |
delete[] test_suites; |
| 124 |
|
| 125 |
// The following lines initiate actions to verify certain methods in |
| 126 |
// FinalSuccessChecker::TearDown. |
| 127 |
|
| 128 |
// Records a test property to verify TestResult::GetTestProperty(). |
| 129 |
RecordProperty("key", "value"); |
| 130 |
} |
| 131 |
|
| 132 |
AssertionResult IsNull(const char* str) { |
| 133 |
if (str != nullptr) { |
| 134 |
return testing::AssertionFailure() << "argument is " << str; |
| 135 |
} |
| 136 |
return AssertionSuccess(); |
| 137 |
} |
| 138 |
|
| 139 |
TEST(ApiTest, TestSuiteImmutableAccessorsWork) { |
| 140 |
const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest"); |
| 141 |
ASSERT_TRUE(test_suite != nullptr); |
| 142 |
|
| 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()); |
| 149 |
|
| 150 |
const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite); |
| 151 |
|
| 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()); |
| 157 |
|
| 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()); |
| 163 |
|
| 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()); |
| 169 |
|
| 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()); |
| 175 |
|
| 176 |
delete[] tests; |
| 177 |
tests = nullptr; |
| 178 |
|
| 179 |
test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0"); |
| 180 |
ASSERT_TRUE(test_suite != nullptr); |
| 181 |
|
| 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()); |
| 188 |
|
| 189 |
tests = UnitTestHelper::GetSortedTests(test_suite); |
| 190 |
|
| 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()); |
| 196 |
|
| 197 |
delete[] tests; |
| 198 |
} |
| 199 |
|
| 200 |
TEST(ApiTest, TestSuiteDisabledAccessorsWork) { |
| 201 |
const TestSuite* test_suite = UnitTestHelper::FindTestSuite("DISABLED_Test"); |
| 202 |
ASSERT_TRUE(test_suite != nullptr); |
| 203 |
|
| 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()); |
| 210 |
|
| 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()); |
| 217 |
} |
| 218 |
|
| 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) {} |
| 223 |
|
| 224 |
class FinalSuccessChecker : public Environment { |
| 225 |
protected: |
| 226 |
void TearDown() override { |
| 227 |
UnitTest* unit_test = UnitTest::GetInstance(); |
| 228 |
|
| 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()); |
| 236 |
|
| 237 |
const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites(); |
| 238 |
|
| 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()); |
| 248 |
|
| 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()); |
| 256 |
|
| 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()); |
| 267 |
|
| 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()); |
| 273 |
|
| 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()); |
| 281 |
|
| 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()); |
| 289 |
|
| 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()); |
| 300 |
|
| 301 |
delete[] tests; |
| 302 |
|
| 303 |
test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0"); |
| 304 |
tests = UnitTestHelper::GetSortedTests(test_suite); |
| 305 |
|
| 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()); |
| 313 |
|
| 314 |
delete[] tests; |
| 315 |
delete[] test_suites; |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
} // namespace internal |
| 320 |
} // namespace testing |
| 321 |
|
| 322 |
int main(int argc, char **argv) { |
| 323 |
InitGoogleTest(&argc, argv); |
| 324 |
|
| 325 |
AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker()); |
| 326 |
|
| 327 |
return RUN_ALL_TESTS(); |
| 328 |
} |