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.

340 lines
13 KiB

  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 =
  52. new const TestSuite*[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 =
  76. new const TestInfo*[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. #if GTEST_HAS_TYPED_TEST
  85. template <typename T> class TestSuiteWithCommentTest : public Test {};
  86. TYPED_TEST_SUITE(TestSuiteWithCommentTest, Types<int>);
  87. TYPED_TEST(TestSuiteWithCommentTest, Dummy) {}
  88. const int kTypedTestSuites = 1;
  89. const int kTypedTests = 1;
  90. #else
  91. const int kTypedTestSuites = 0;
  92. const int kTypedTests = 0;
  93. #endif // GTEST_HAS_TYPED_TEST
  94. // We can only test the accessors that do not change value while tests run.
  95. // Since tests can be run in any order, the values the accessors that track
  96. // test execution (such as failed_test_count) can not be predicted.
  97. TEST(ApiTest, UnitTestImmutableAccessorsWork) {
  98. UnitTest* unit_test = UnitTest::GetInstance();
  99. ASSERT_EQ(2 + kTypedTestSuites, unit_test->total_test_suite_count());
  100. EXPECT_EQ(1 + kTypedTestSuites, unit_test->test_suite_to_run_count());
  101. EXPECT_EQ(2, unit_test->disabled_test_count());
  102. EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
  103. EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
  104. const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
  105. EXPECT_STREQ("ApiTest", test_suites[0]->name());
  106. EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
  107. #if GTEST_HAS_TYPED_TEST
  108. EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
  109. #endif // GTEST_HAS_TYPED_TEST
  110. delete[] test_suites;
  111. // The following lines initiate actions to verify certain methods in
  112. // FinalSuccessChecker::TearDown.
  113. // Records a test property to verify TestResult::GetTestProperty().
  114. RecordProperty("key", "value");
  115. }
  116. AssertionResult IsNull(const char* str) {
  117. if (str != nullptr) {
  118. return testing::AssertionFailure() << "argument is " << str;
  119. }
  120. return AssertionSuccess();
  121. }
  122. TEST(ApiTest, TestSuiteImmutableAccessorsWork) {
  123. const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest");
  124. ASSERT_TRUE(test_suite != nullptr);
  125. EXPECT_STREQ("ApiTest", test_suite->name());
  126. EXPECT_TRUE(IsNull(test_suite->type_param()));
  127. EXPECT_TRUE(test_suite->should_run());
  128. EXPECT_EQ(1, test_suite->disabled_test_count());
  129. EXPECT_EQ(3, test_suite->test_to_run_count());
  130. ASSERT_EQ(4, test_suite->total_test_count());
  131. const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
  132. EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
  133. EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
  134. EXPECT_TRUE(IsNull(tests[0]->value_param()));
  135. EXPECT_TRUE(IsNull(tests[0]->type_param()));
  136. EXPECT_FALSE(tests[0]->should_run());
  137. EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
  138. EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
  139. EXPECT_TRUE(IsNull(tests[1]->value_param()));
  140. EXPECT_TRUE(IsNull(tests[1]->type_param()));
  141. EXPECT_TRUE(tests[1]->should_run());
  142. EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
  143. EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
  144. EXPECT_TRUE(IsNull(tests[2]->value_param()));
  145. EXPECT_TRUE(IsNull(tests[2]->type_param()));
  146. EXPECT_TRUE(tests[2]->should_run());
  147. EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
  148. EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
  149. EXPECT_TRUE(IsNull(tests[3]->value_param()));
  150. EXPECT_TRUE(IsNull(tests[3]->type_param()));
  151. EXPECT_TRUE(tests[3]->should_run());
  152. delete[] tests;
  153. tests = nullptr;
  154. #if GTEST_HAS_TYPED_TEST
  155. test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
  156. ASSERT_TRUE(test_suite != nullptr);
  157. EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite->name());
  158. EXPECT_STREQ(GetTypeName<int>().c_str(), test_suite->type_param());
  159. EXPECT_TRUE(test_suite->should_run());
  160. EXPECT_EQ(0, test_suite->disabled_test_count());
  161. EXPECT_EQ(1, test_suite->test_to_run_count());
  162. ASSERT_EQ(1, test_suite->total_test_count());
  163. tests = UnitTestHelper::GetSortedTests(test_suite);
  164. EXPECT_STREQ("Dummy", tests[0]->name());
  165. EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
  166. EXPECT_TRUE(IsNull(tests[0]->value_param()));
  167. EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
  168. EXPECT_TRUE(tests[0]->should_run());
  169. delete[] tests;
  170. #endif // GTEST_HAS_TYPED_TEST
  171. }
  172. TEST(ApiTest, TestSuiteDisabledAccessorsWork) {
  173. const TestSuite* test_suite = UnitTestHelper::FindTestSuite("DISABLED_Test");
  174. ASSERT_TRUE(test_suite != nullptr);
  175. EXPECT_STREQ("DISABLED_Test", test_suite->name());
  176. EXPECT_TRUE(IsNull(test_suite->type_param()));
  177. EXPECT_FALSE(test_suite->should_run());
  178. EXPECT_EQ(1, test_suite->disabled_test_count());
  179. EXPECT_EQ(0, test_suite->test_to_run_count());
  180. ASSERT_EQ(1, test_suite->total_test_count());
  181. const TestInfo* const test_info = test_suite->GetTestInfo(0);
  182. EXPECT_STREQ("Dummy2", test_info->name());
  183. EXPECT_STREQ("DISABLED_Test", test_info->test_suite_name());
  184. EXPECT_TRUE(IsNull(test_info->value_param()));
  185. EXPECT_TRUE(IsNull(test_info->type_param()));
  186. EXPECT_FALSE(test_info->should_run());
  187. }
  188. // These two tests are here to provide support for testing
  189. // test_suite_to_run_count, disabled_test_count, and test_to_run_count.
  190. TEST(ApiTest, DISABLED_Dummy1) {}
  191. TEST(DISABLED_Test, Dummy2) {}
  192. class FinalSuccessChecker : public Environment {
  193. protected:
  194. void TearDown() override {
  195. UnitTest* unit_test = UnitTest::GetInstance();
  196. EXPECT_EQ(1 + kTypedTestSuites, unit_test->successful_test_suite_count());
  197. EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
  198. EXPECT_EQ(0, unit_test->failed_test_suite_count());
  199. EXPECT_EQ(0, unit_test->failed_test_count());
  200. EXPECT_TRUE(unit_test->Passed());
  201. EXPECT_FALSE(unit_test->Failed());
  202. ASSERT_EQ(2 + kTypedTestSuites, unit_test->total_test_suite_count());
  203. const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
  204. EXPECT_STREQ("ApiTest", test_suites[0]->name());
  205. EXPECT_TRUE(IsNull(test_suites[0]->type_param()));
  206. EXPECT_TRUE(test_suites[0]->should_run());
  207. EXPECT_EQ(1, test_suites[0]->disabled_test_count());
  208. ASSERT_EQ(4, test_suites[0]->total_test_count());
  209. EXPECT_EQ(3, test_suites[0]->successful_test_count());
  210. EXPECT_EQ(0, test_suites[0]->failed_test_count());
  211. EXPECT_TRUE(test_suites[0]->Passed());
  212. EXPECT_FALSE(test_suites[0]->Failed());
  213. EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
  214. EXPECT_TRUE(IsNull(test_suites[1]->type_param()));
  215. EXPECT_FALSE(test_suites[1]->should_run());
  216. EXPECT_EQ(1, test_suites[1]->disabled_test_count());
  217. ASSERT_EQ(1, test_suites[1]->total_test_count());
  218. EXPECT_EQ(0, test_suites[1]->successful_test_count());
  219. EXPECT_EQ(0, test_suites[1]->failed_test_count());
  220. #if GTEST_HAS_TYPED_TEST
  221. EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
  222. EXPECT_STREQ(GetTypeName<int>().c_str(), test_suites[2]->type_param());
  223. EXPECT_TRUE(test_suites[2]->should_run());
  224. EXPECT_EQ(0, test_suites[2]->disabled_test_count());
  225. ASSERT_EQ(1, test_suites[2]->total_test_count());
  226. EXPECT_EQ(1, test_suites[2]->successful_test_count());
  227. EXPECT_EQ(0, test_suites[2]->failed_test_count());
  228. EXPECT_TRUE(test_suites[2]->Passed());
  229. EXPECT_FALSE(test_suites[2]->Failed());
  230. #endif // GTEST_HAS_TYPED_TEST
  231. const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest");
  232. const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
  233. EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
  234. EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
  235. EXPECT_FALSE(tests[0]->should_run());
  236. EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
  237. EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
  238. EXPECT_TRUE(IsNull(tests[1]->value_param()));
  239. EXPECT_TRUE(IsNull(tests[1]->type_param()));
  240. EXPECT_TRUE(tests[1]->should_run());
  241. EXPECT_TRUE(tests[1]->result()->Passed());
  242. EXPECT_EQ(0, tests[1]->result()->test_property_count());
  243. EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
  244. EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
  245. EXPECT_TRUE(IsNull(tests[2]->value_param()));
  246. EXPECT_TRUE(IsNull(tests[2]->type_param()));
  247. EXPECT_TRUE(tests[2]->should_run());
  248. EXPECT_TRUE(tests[2]->result()->Passed());
  249. EXPECT_EQ(0, tests[2]->result()->test_property_count());
  250. EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
  251. EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
  252. EXPECT_TRUE(IsNull(tests[3]->value_param()));
  253. EXPECT_TRUE(IsNull(tests[3]->type_param()));
  254. EXPECT_TRUE(tests[3]->should_run());
  255. EXPECT_TRUE(tests[3]->result()->Passed());
  256. EXPECT_EQ(1, tests[3]->result()->test_property_count());
  257. const TestProperty& property = tests[3]->result()->GetTestProperty(0);
  258. EXPECT_STREQ("key", property.key());
  259. EXPECT_STREQ("value", property.value());
  260. delete[] tests;
  261. #if GTEST_HAS_TYPED_TEST
  262. test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
  263. tests = UnitTestHelper::GetSortedTests(test_suite);
  264. EXPECT_STREQ("Dummy", tests[0]->name());
  265. EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
  266. EXPECT_TRUE(IsNull(tests[0]->value_param()));
  267. EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
  268. EXPECT_TRUE(tests[0]->should_run());
  269. EXPECT_TRUE(tests[0]->result()->Passed());
  270. EXPECT_EQ(0, tests[0]->result()->test_property_count());
  271. delete[] tests;
  272. #endif // GTEST_HAS_TYPED_TEST
  273. delete[] test_suites;
  274. }
  275. };
  276. } // namespace internal
  277. } // namespace testing
  278. int main(int argc, char **argv) {
  279. InitGoogleTest(&argc, argv);
  280. AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
  281. return RUN_ALL_TESTS();
  282. }