media/webrtc/trunk/testing/gtest/test/gtest-unittest-api_test.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // Copyright 2009 Google Inc. All rights reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28 //
michael@0 29 // Author: vladl@google.com (Vlad Losev)
michael@0 30 //
michael@0 31 // The Google C++ Testing Framework (Google Test)
michael@0 32 //
michael@0 33 // This file contains tests verifying correctness of data provided via
michael@0 34 // UnitTest's public methods.
michael@0 35
michael@0 36 #include "gtest/gtest.h"
michael@0 37
michael@0 38 #include <string.h> // For strcmp.
michael@0 39 #include <algorithm>
michael@0 40
michael@0 41 using ::testing::InitGoogleTest;
michael@0 42
michael@0 43 namespace testing {
michael@0 44 namespace internal {
michael@0 45
michael@0 46 template <typename T>
michael@0 47 struct LessByName {
michael@0 48 bool operator()(const T* a, const T* b) {
michael@0 49 return strcmp(a->name(), b->name()) < 0;
michael@0 50 }
michael@0 51 };
michael@0 52
michael@0 53 class UnitTestHelper {
michael@0 54 public:
michael@0 55 // Returns the array of pointers to all test cases sorted by the test case
michael@0 56 // name. The caller is responsible for deleting the array.
michael@0 57 static TestCase const** const GetSortedTestCases() {
michael@0 58 UnitTest& unit_test = *UnitTest::GetInstance();
michael@0 59 TestCase const** const test_cases =
michael@0 60 new const TestCase*[unit_test.total_test_case_count()];
michael@0 61
michael@0 62 for (int i = 0; i < unit_test.total_test_case_count(); ++i)
michael@0 63 test_cases[i] = unit_test.GetTestCase(i);
michael@0 64
michael@0 65 std::sort(test_cases,
michael@0 66 test_cases + unit_test.total_test_case_count(),
michael@0 67 LessByName<TestCase>());
michael@0 68 return test_cases;
michael@0 69 }
michael@0 70
michael@0 71 // Returns the test case by its name. The caller doesn't own the returned
michael@0 72 // pointer.
michael@0 73 static const TestCase* FindTestCase(const char* name) {
michael@0 74 UnitTest& unit_test = *UnitTest::GetInstance();
michael@0 75 for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
michael@0 76 const TestCase* test_case = unit_test.GetTestCase(i);
michael@0 77 if (0 == strcmp(test_case->name(), name))
michael@0 78 return test_case;
michael@0 79 }
michael@0 80 return NULL;
michael@0 81 }
michael@0 82
michael@0 83 // Returns the array of pointers to all tests in a particular test case
michael@0 84 // sorted by the test name. The caller is responsible for deleting the
michael@0 85 // array.
michael@0 86 static TestInfo const** const GetSortedTests(const TestCase* test_case) {
michael@0 87 TestInfo const** const tests =
michael@0 88 new const TestInfo*[test_case->total_test_count()];
michael@0 89
michael@0 90 for (int i = 0; i < test_case->total_test_count(); ++i)
michael@0 91 tests[i] = test_case->GetTestInfo(i);
michael@0 92
michael@0 93 std::sort(tests, tests + test_case->total_test_count(),
michael@0 94 LessByName<TestInfo>());
michael@0 95 return tests;
michael@0 96 }
michael@0 97 };
michael@0 98
michael@0 99 #if GTEST_HAS_TYPED_TEST
michael@0 100 template <typename T> class TestCaseWithCommentTest : public Test {};
michael@0 101 TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
michael@0 102 TYPED_TEST(TestCaseWithCommentTest, Dummy) {}
michael@0 103
michael@0 104 const int kTypedTestCases = 1;
michael@0 105 const int kTypedTests = 1;
michael@0 106 #else
michael@0 107 const int kTypedTestCases = 0;
michael@0 108 const int kTypedTests = 0;
michael@0 109 #endif // GTEST_HAS_TYPED_TEST
michael@0 110
michael@0 111 // We can only test the accessors that do not change value while tests run.
michael@0 112 // Since tests can be run in any order, the values the accessors that track
michael@0 113 // test execution (such as failed_test_count) can not be predicted.
michael@0 114 TEST(ApiTest, UnitTestImmutableAccessorsWork) {
michael@0 115 UnitTest* unit_test = UnitTest::GetInstance();
michael@0 116
michael@0 117 ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
michael@0 118 EXPECT_EQ(1 + kTypedTestCases, unit_test->test_case_to_run_count());
michael@0 119 EXPECT_EQ(2, unit_test->disabled_test_count());
michael@0 120 EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
michael@0 121 EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
michael@0 122
michael@0 123 const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
michael@0 124
michael@0 125 EXPECT_STREQ("ApiTest", test_cases[0]->name());
michael@0 126 EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
michael@0 127 #if GTEST_HAS_TYPED_TEST
michael@0 128 EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
michael@0 129 #endif // GTEST_HAS_TYPED_TEST
michael@0 130
michael@0 131 delete[] test_cases;
michael@0 132
michael@0 133 // The following lines initiate actions to verify certain methods in
michael@0 134 // FinalSuccessChecker::TearDown.
michael@0 135
michael@0 136 // Records a test property to verify TestResult::GetTestProperty().
michael@0 137 RecordProperty("key", "value");
michael@0 138 }
michael@0 139
michael@0 140 AssertionResult IsNull(const char* str) {
michael@0 141 if (str != NULL) {
michael@0 142 return testing::AssertionFailure() << "argument is " << str;
michael@0 143 }
michael@0 144 return AssertionSuccess();
michael@0 145 }
michael@0 146
michael@0 147 TEST(ApiTest, TestCaseImmutableAccessorsWork) {
michael@0 148 const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
michael@0 149 ASSERT_TRUE(test_case != NULL);
michael@0 150
michael@0 151 EXPECT_STREQ("ApiTest", test_case->name());
michael@0 152 EXPECT_TRUE(IsNull(test_case->type_param()));
michael@0 153 EXPECT_TRUE(test_case->should_run());
michael@0 154 EXPECT_EQ(1, test_case->disabled_test_count());
michael@0 155 EXPECT_EQ(3, test_case->test_to_run_count());
michael@0 156 ASSERT_EQ(4, test_case->total_test_count());
michael@0 157
michael@0 158 const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
michael@0 159
michael@0 160 EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
michael@0 161 EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
michael@0 162 EXPECT_TRUE(IsNull(tests[0]->value_param()));
michael@0 163 EXPECT_TRUE(IsNull(tests[0]->type_param()));
michael@0 164 EXPECT_FALSE(tests[0]->should_run());
michael@0 165
michael@0 166 EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
michael@0 167 EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
michael@0 168 EXPECT_TRUE(IsNull(tests[1]->value_param()));
michael@0 169 EXPECT_TRUE(IsNull(tests[1]->type_param()));
michael@0 170 EXPECT_TRUE(tests[1]->should_run());
michael@0 171
michael@0 172 EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
michael@0 173 EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
michael@0 174 EXPECT_TRUE(IsNull(tests[2]->value_param()));
michael@0 175 EXPECT_TRUE(IsNull(tests[2]->type_param()));
michael@0 176 EXPECT_TRUE(tests[2]->should_run());
michael@0 177
michael@0 178 EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
michael@0 179 EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
michael@0 180 EXPECT_TRUE(IsNull(tests[3]->value_param()));
michael@0 181 EXPECT_TRUE(IsNull(tests[3]->type_param()));
michael@0 182 EXPECT_TRUE(tests[3]->should_run());
michael@0 183
michael@0 184 delete[] tests;
michael@0 185 tests = NULL;
michael@0 186
michael@0 187 #if GTEST_HAS_TYPED_TEST
michael@0 188 test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
michael@0 189 ASSERT_TRUE(test_case != NULL);
michael@0 190
michael@0 191 EXPECT_STREQ("TestCaseWithCommentTest/0", test_case->name());
michael@0 192 EXPECT_STREQ(GetTypeName<int>().c_str(), test_case->type_param());
michael@0 193 EXPECT_TRUE(test_case->should_run());
michael@0 194 EXPECT_EQ(0, test_case->disabled_test_count());
michael@0 195 EXPECT_EQ(1, test_case->test_to_run_count());
michael@0 196 ASSERT_EQ(1, test_case->total_test_count());
michael@0 197
michael@0 198 tests = UnitTestHelper::GetSortedTests(test_case);
michael@0 199
michael@0 200 EXPECT_STREQ("Dummy", tests[0]->name());
michael@0 201 EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
michael@0 202 EXPECT_TRUE(IsNull(tests[0]->value_param()));
michael@0 203 EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
michael@0 204 EXPECT_TRUE(tests[0]->should_run());
michael@0 205
michael@0 206 delete[] tests;
michael@0 207 #endif // GTEST_HAS_TYPED_TEST
michael@0 208 }
michael@0 209
michael@0 210 TEST(ApiTest, TestCaseDisabledAccessorsWork) {
michael@0 211 const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
michael@0 212 ASSERT_TRUE(test_case != NULL);
michael@0 213
michael@0 214 EXPECT_STREQ("DISABLED_Test", test_case->name());
michael@0 215 EXPECT_TRUE(IsNull(test_case->type_param()));
michael@0 216 EXPECT_FALSE(test_case->should_run());
michael@0 217 EXPECT_EQ(1, test_case->disabled_test_count());
michael@0 218 EXPECT_EQ(0, test_case->test_to_run_count());
michael@0 219 ASSERT_EQ(1, test_case->total_test_count());
michael@0 220
michael@0 221 const TestInfo* const test_info = test_case->GetTestInfo(0);
michael@0 222 EXPECT_STREQ("Dummy2", test_info->name());
michael@0 223 EXPECT_STREQ("DISABLED_Test", test_info->test_case_name());
michael@0 224 EXPECT_TRUE(IsNull(test_info->value_param()));
michael@0 225 EXPECT_TRUE(IsNull(test_info->type_param()));
michael@0 226 EXPECT_FALSE(test_info->should_run());
michael@0 227 }
michael@0 228
michael@0 229 // These two tests are here to provide support for testing
michael@0 230 // test_case_to_run_count, disabled_test_count, and test_to_run_count.
michael@0 231 TEST(ApiTest, DISABLED_Dummy1) {}
michael@0 232 TEST(DISABLED_Test, Dummy2) {}
michael@0 233
michael@0 234 class FinalSuccessChecker : public Environment {
michael@0 235 protected:
michael@0 236 virtual void TearDown() {
michael@0 237 UnitTest* unit_test = UnitTest::GetInstance();
michael@0 238
michael@0 239 EXPECT_EQ(1 + kTypedTestCases, unit_test->successful_test_case_count());
michael@0 240 EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
michael@0 241 EXPECT_EQ(0, unit_test->failed_test_case_count());
michael@0 242 EXPECT_EQ(0, unit_test->failed_test_count());
michael@0 243 EXPECT_TRUE(unit_test->Passed());
michael@0 244 EXPECT_FALSE(unit_test->Failed());
michael@0 245 ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
michael@0 246
michael@0 247 const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
michael@0 248
michael@0 249 EXPECT_STREQ("ApiTest", test_cases[0]->name());
michael@0 250 EXPECT_TRUE(IsNull(test_cases[0]->type_param()));
michael@0 251 EXPECT_TRUE(test_cases[0]->should_run());
michael@0 252 EXPECT_EQ(1, test_cases[0]->disabled_test_count());
michael@0 253 ASSERT_EQ(4, test_cases[0]->total_test_count());
michael@0 254 EXPECT_EQ(3, test_cases[0]->successful_test_count());
michael@0 255 EXPECT_EQ(0, test_cases[0]->failed_test_count());
michael@0 256 EXPECT_TRUE(test_cases[0]->Passed());
michael@0 257 EXPECT_FALSE(test_cases[0]->Failed());
michael@0 258
michael@0 259 EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
michael@0 260 EXPECT_TRUE(IsNull(test_cases[1]->type_param()));
michael@0 261 EXPECT_FALSE(test_cases[1]->should_run());
michael@0 262 EXPECT_EQ(1, test_cases[1]->disabled_test_count());
michael@0 263 ASSERT_EQ(1, test_cases[1]->total_test_count());
michael@0 264 EXPECT_EQ(0, test_cases[1]->successful_test_count());
michael@0 265 EXPECT_EQ(0, test_cases[1]->failed_test_count());
michael@0 266
michael@0 267 #if GTEST_HAS_TYPED_TEST
michael@0 268 EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
michael@0 269 EXPECT_STREQ(GetTypeName<int>().c_str(), test_cases[2]->type_param());
michael@0 270 EXPECT_TRUE(test_cases[2]->should_run());
michael@0 271 EXPECT_EQ(0, test_cases[2]->disabled_test_count());
michael@0 272 ASSERT_EQ(1, test_cases[2]->total_test_count());
michael@0 273 EXPECT_EQ(1, test_cases[2]->successful_test_count());
michael@0 274 EXPECT_EQ(0, test_cases[2]->failed_test_count());
michael@0 275 EXPECT_TRUE(test_cases[2]->Passed());
michael@0 276 EXPECT_FALSE(test_cases[2]->Failed());
michael@0 277 #endif // GTEST_HAS_TYPED_TEST
michael@0 278
michael@0 279 const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
michael@0 280 const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
michael@0 281 EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
michael@0 282 EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
michael@0 283 EXPECT_FALSE(tests[0]->should_run());
michael@0 284
michael@0 285 EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
michael@0 286 EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
michael@0 287 EXPECT_TRUE(IsNull(tests[1]->value_param()));
michael@0 288 EXPECT_TRUE(IsNull(tests[1]->type_param()));
michael@0 289 EXPECT_TRUE(tests[1]->should_run());
michael@0 290 EXPECT_TRUE(tests[1]->result()->Passed());
michael@0 291 EXPECT_EQ(0, tests[1]->result()->test_property_count());
michael@0 292
michael@0 293 EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
michael@0 294 EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
michael@0 295 EXPECT_TRUE(IsNull(tests[2]->value_param()));
michael@0 296 EXPECT_TRUE(IsNull(tests[2]->type_param()));
michael@0 297 EXPECT_TRUE(tests[2]->should_run());
michael@0 298 EXPECT_TRUE(tests[2]->result()->Passed());
michael@0 299 EXPECT_EQ(0, tests[2]->result()->test_property_count());
michael@0 300
michael@0 301 EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
michael@0 302 EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
michael@0 303 EXPECT_TRUE(IsNull(tests[3]->value_param()));
michael@0 304 EXPECT_TRUE(IsNull(tests[3]->type_param()));
michael@0 305 EXPECT_TRUE(tests[3]->should_run());
michael@0 306 EXPECT_TRUE(tests[3]->result()->Passed());
michael@0 307 EXPECT_EQ(1, tests[3]->result()->test_property_count());
michael@0 308 const TestProperty& property = tests[3]->result()->GetTestProperty(0);
michael@0 309 EXPECT_STREQ("key", property.key());
michael@0 310 EXPECT_STREQ("value", property.value());
michael@0 311
michael@0 312 delete[] tests;
michael@0 313
michael@0 314 #if GTEST_HAS_TYPED_TEST
michael@0 315 test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
michael@0 316 tests = UnitTestHelper::GetSortedTests(test_case);
michael@0 317
michael@0 318 EXPECT_STREQ("Dummy", tests[0]->name());
michael@0 319 EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
michael@0 320 EXPECT_TRUE(IsNull(tests[0]->value_param()));
michael@0 321 EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
michael@0 322 EXPECT_TRUE(tests[0]->should_run());
michael@0 323 EXPECT_TRUE(tests[0]->result()->Passed());
michael@0 324 EXPECT_EQ(0, tests[0]->result()->test_property_count());
michael@0 325
michael@0 326 delete[] tests;
michael@0 327 #endif // GTEST_HAS_TYPED_TEST
michael@0 328 delete[] test_cases;
michael@0 329 }
michael@0 330 };
michael@0 331
michael@0 332 } // namespace internal
michael@0 333 } // namespace testing
michael@0 334
michael@0 335 int main(int argc, char **argv) {
michael@0 336 InitGoogleTest(&argc, argv);
michael@0 337
michael@0 338 AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
michael@0 339
michael@0 340 return RUN_ALL_TESTS();
michael@0 341 }

mercurial