You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
13 KiB

2 months ago
  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. // The Google C++ Testing and Mocking Framework (Google Test)
  30. //
  31. // This file contains tests verifying correctness of data provided via
  32. // UnitTest's public methods.
  33. #include "gtest/gtest.h"
  34. #include <string.h> // For strcmp.
  35. #include <algorithm>
  36. using ::testing::InitGoogleTest;
  37. namespace testing {
  38. namespace internal {
  39. template <typename T>
  40. struct LessByName {
  41. bool operator()(const T* a, const T* b) {
  42. return strcmp(a->name(), b->name()) < 0;
  43. }
  44. };
  45. class UnitTestHelper {
  46. public:
  47. // Returns the array of pointers to all test suites sorted by the test suite
  48. // name. The caller is responsible for deleting the array.
  49. static TestSuite const** GetSortedTestSuites() {
  50. UnitTest& unit_test = *UnitTest::GetInstance();
  51. auto const** const test_suites = new const TestSuite*[static_cast<size_t>(
  52. unit_test.total_test_suite_count())];
  53. for (int i = 0; i < unit_test.total_test_suite_count(); ++i)
  54. test_suites[i] = unit_test.GetTestSuite(i);
  55. std::sort(test_suites,
  56. test_suites + unit_test.total_test_suite_count(),
  57. LessByName<TestSuite>());
  58. return test_suites;
  59. }
  60. // Returns the test suite by its name. The caller doesn't own the returned
  61. // pointer.
  62. static const TestSuite* FindTestSuite(const char* name) {
  63. UnitTest& unit_test = *UnitTest::GetInstance();
  64. for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
  65. const TestSuite* test_suite = unit_test.GetTestSuite(i);
  66. if (0 == strcmp(test_suite->name(), name))
  67. return test_suite;
  68. }
  69. return nullptr;
  70. }
  71. // Returns the array of pointers to all tests in a particular test suite
  72. // sorted by the test name. The caller is responsible for deleting the
  73. // array.
  74. static TestInfo const** GetSortedTests(const TestSuite* test_suite) {
  75. TestInfo const** const tests = new const TestInfo*[static_cast<size_t>(
  76. test_suite->total_test_count())];
  77. for (int i = 0; i < test_suite->total_test_count(); ++i)
  78. tests[i] = test_suite->GetTestInfo(i);
  79. std::sort(tests, tests + test_suite->total_test_count(),
  80. LessByName<TestInfo>());
  81. return tests;
  82. }
  83. };
  84. template <typename T> class TestSuiteWithCommentTest : public Test {};
  85. TYPED_TEST_SUITE(TestSuiteWithCommentTest, Types<int>);
  86. TYPED_TEST(TestSuiteWithCommentTest, Dummy) {}
  87. const int kTypedTestSuites = 1;
  88. const int kTypedTests = 1;
  89. // We can only test the accessors that do not change value while tests run.
  90. // Since tests can be run in any order, the values the accessors that track
  91. // test execution (such as failed_test_count) can not be predicted.
  92. TEST(ApiTest, UnitTestImmutableAccessorsWork) {
  93. UnitTest* unit_test = UnitTest::GetInstance();
  94. ASSERT_EQ(2 + kTypedTestSuites, unit_test->total_test_suite_count());
  95. EXPECT_EQ(1 + kTypedTestSuites, unit_test->test_suite_to_run_count());
  96. EXPECT_EQ(2, unit_test->disabled_test_count());
  97. EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
  98. EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
  99. const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
  100. EXPECT_STREQ("ApiTest", test_suites[0]->name());
  101. EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
  102. EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
  103. delete[] test_suites;
  104. // The following lines initiate actions to verify certain methods in
  105. // FinalSuccessChecker::TearDown.
  106. // Records a test property to verify TestResult::GetTestProperty().
  107. RecordProperty("key", "value");
  108. }
  109. AssertionResult IsNull(const char* str) {
  110. if (str != nullptr) {
  111. return testing::AssertionFailure() << "argument is " << str;
  112. }
  113. return AssertionSuccess();
  114. }
  115. TEST(ApiTest, TestSuiteImmutableAccessorsWork) {
  116. const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest");
  117. ASSERT_TRUE(test_suite != nullptr);
  118. EXPECT_STREQ("ApiTest", test_suite->name());
  119. EXPECT_TRUE(IsNull(test_suite->type_param()));
  120. EXPECT_TRUE(test_suite->should_run());
  121. EXPECT_EQ(1, test_suite->disabled_test_count());
  122. EXPECT_EQ(3, test_suite->test_to_run_count());
  123. ASSERT_EQ(4, test_suite->total_test_count());
  124. const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
  125. EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
  126. EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
  127. EXPECT_TRUE(IsNull(tests[0]->value_param()));
  128. EXPECT_TRUE(IsNull(tests[0]->type_param()));
  129. EXPECT_FALSE(tests[0]->should_run());
  130. EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
  131. EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
  132. EXPECT_TRUE(IsNull(tests[1]->value_param()));
  133. EXPECT_TRUE(IsNull(tests[1]->type_param()));
  134. EXPECT_TRUE(tests[1]->should_run());
  135. EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
  136. EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
  137. EXPECT_TRUE(IsNull(tests[2]->value_param()));
  138. EXPECT_TRUE(IsNull(tests[2]->type_param()));
  139. EXPECT_TRUE(tests[2]->should_run());
  140. EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
  141. EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
  142. EXPECT_TRUE(IsNull(tests[3]->value_param()));
  143. EXPECT_TRUE(IsNull(tests[3]->type_param()));
  144. EXPECT_TRUE(tests[3]->should_run());
  145. delete[] tests;
  146. tests = nullptr;
  147. test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
  148. ASSERT_TRUE(test_suite != nullptr);
  149. EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite->name());
  150. EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), test_suite->type_param());
  151. EXPECT_TRUE(test_suite->should_run());
  152. EXPECT_EQ(0, test_suite->disabled_test_count());
  153. EXPECT_EQ(1, test_suite->test_to_run_count());
  154. ASSERT_EQ(1, test_suite->total_test_count());
  155. tests = UnitTestHelper::GetSortedTests(test_suite);
  156. EXPECT_STREQ("Dummy", tests[0]->name());
  157. EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
  158. EXPECT_TRUE(IsNull(tests[0]->value_param()));
  159. EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), tests[0]->type_param());
  160. EXPECT_TRUE(tests[0]->should_run());
  161. delete[] tests;
  162. }
  163. TEST(ApiTest, TestSuiteDisabledAccessorsWork) {
  164. const TestSuite* test_suite = UnitTestHelper::FindTestSuite("DISABLED_Test");
  165. ASSERT_TRUE(test_suite != nullptr);
  166. EXPECT_STREQ("DISABLED_Test", test_suite->name());
  167. EXPECT_TRUE(IsNull(test_suite->type_param()));
  168. EXPECT_FALSE(test_suite->should_run());
  169. EXPECT_EQ(1, test_suite->disabled_test_count());
  170. EXPECT_EQ(0, test_suite->test_to_run_count());
  171. ASSERT_EQ(1, test_suite->total_test_count());
  172. const TestInfo* const test_info = test_suite->GetTestInfo(0);
  173. EXPECT_STREQ("Dummy2", test_info->name());
  174. EXPECT_STREQ("DISABLED_Test", test_info->test_suite_name());
  175. EXPECT_TRUE(IsNull(test_info->value_param()));
  176. EXPECT_TRUE(IsNull(test_info->type_param()));
  177. EXPECT_FALSE(test_info->should_run());
  178. }
  179. // These two tests are here to provide support for testing
  180. // test_suite_to_run_count, disabled_test_count, and test_to_run_count.
  181. TEST(ApiTest, DISABLED_Dummy1) {}
  182. TEST(DISABLED_Test, Dummy2) {}
  183. class FinalSuccessChecker : public Environment {
  184. protected:
  185. void TearDown() override {
  186. UnitTest* unit_test = UnitTest::GetInstance();
  187. EXPECT_EQ(1 + kTypedTestSuites, unit_test->successful_test_suite_count());
  188. EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
  189. EXPECT_EQ(0, unit_test->failed_test_suite_count());
  190. EXPECT_EQ(0, unit_test->failed_test_count());
  191. EXPECT_TRUE(unit_test->Passed());
  192. EXPECT_FALSE(unit_test->Failed());
  193. ASSERT_EQ(2 + kTypedTestSuites, unit_test->total_test_suite_count());
  194. const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
  195. EXPECT_STREQ("ApiTest", test_suites[0]->name());
  196. EXPECT_TRUE(IsNull(test_suites[0]->type_param()));
  197. EXPECT_TRUE(test_suites[0]->should_run());
  198. EXPECT_EQ(1, test_suites[0]->disabled_test_count());
  199. ASSERT_EQ(4, test_suites[0]->total_test_count());
  200. EXPECT_EQ(3, test_suites[0]->successful_test_count());
  201. EXPECT_EQ(0, test_suites[0]->failed_test_count());
  202. EXPECT_TRUE(test_suites[0]->Passed());
  203. EXPECT_FALSE(test_suites[0]->Failed());
  204. EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
  205. EXPECT_TRUE(IsNull(test_suites[1]->type_param()));
  206. EXPECT_FALSE(test_suites[1]->should_run());
  207. EXPECT_EQ(1, test_suites[1]->disabled_test_count());
  208. ASSERT_EQ(1, test_suites[1]->total_test_count());
  209. EXPECT_EQ(0, test_suites[1]->successful_test_count());
  210. EXPECT_EQ(0, test_suites[1]->failed_test_count());
  211. EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
  212. EXPECT_STREQ(GetTypeName<Types<int>>().c_str(),
  213. test_suites[2]->type_param());
  214. EXPECT_TRUE(test_suites[2]->should_run());
  215. EXPECT_EQ(0, test_suites[2]->disabled_test_count());
  216. ASSERT_EQ(1, test_suites[2]->total_test_count());
  217. EXPECT_EQ(1, test_suites[2]->successful_test_count());
  218. EXPECT_EQ(0, test_suites[2]->failed_test_count());
  219. EXPECT_TRUE(test_suites[2]->Passed());
  220. EXPECT_FALSE(test_suites[2]->Failed());
  221. const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest");
  222. const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
  223. EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
  224. EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
  225. EXPECT_FALSE(tests[0]->should_run());
  226. EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
  227. EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
  228. EXPECT_TRUE(IsNull(tests[1]->value_param()));
  229. EXPECT_TRUE(IsNull(tests[1]->type_param()));
  230. EXPECT_TRUE(tests[1]->should_run());
  231. EXPECT_TRUE(tests[1]->result()->Passed());
  232. EXPECT_EQ(0, tests[1]->result()->test_property_count());
  233. EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
  234. EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
  235. EXPECT_TRUE(IsNull(tests[2]->value_param()));
  236. EXPECT_TRUE(IsNull(tests[2]->type_param()));
  237. EXPECT_TRUE(tests[2]->should_run());
  238. EXPECT_TRUE(tests[2]->result()->Passed());
  239. EXPECT_EQ(0, tests[2]->result()->test_property_count());
  240. EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
  241. EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
  242. EXPECT_TRUE(IsNull(tests[3]->value_param()));
  243. EXPECT_TRUE(IsNull(tests[3]->type_param()));
  244. EXPECT_TRUE(tests[3]->should_run());
  245. EXPECT_TRUE(tests[3]->result()->Passed());
  246. EXPECT_EQ(1, tests[3]->result()->test_property_count());
  247. const TestProperty& property = tests[3]->result()->GetTestProperty(0);
  248. EXPECT_STREQ("key", property.key());
  249. EXPECT_STREQ("value", property.value());
  250. delete[] tests;
  251. test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
  252. tests = UnitTestHelper::GetSortedTests(test_suite);
  253. EXPECT_STREQ("Dummy", tests[0]->name());
  254. EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
  255. EXPECT_TRUE(IsNull(tests[0]->value_param()));
  256. EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), tests[0]->type_param());
  257. EXPECT_TRUE(tests[0]->should_run());
  258. EXPECT_TRUE(tests[0]->result()->Passed());
  259. EXPECT_EQ(0, tests[0]->result()->test_property_count());
  260. delete[] tests;
  261. delete[] test_suites;
  262. }
  263. };
  264. } // namespace internal
  265. } // namespace testing
  266. int main(int argc, char **argv) {
  267. InitGoogleTest(&argc, argv);
  268. AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
  269. return RUN_ALL_TESTS();
  270. }