gtest-unittest-api_test.cc 13 KB

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