/Unittests/googletest/test/gtest-listener_test.cc

http://unladen-swallow.googlecode.com/ · C++ · 322 lines · 231 code · 43 blank · 48 comment · 4 complexity · 952fc1ce7cd0eb041eb4732916111c38 MD5 · raw file

  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 verifies Google Test event listeners receive events at the
  34. // right times.
  35. #include <gtest/gtest.h>
  36. // Indicates that this translation unit is part of Google Test's
  37. // implementation. It must come before gtest-internal-inl.h is
  38. // included, or there will be a compiler error. This trick is to
  39. // prevent a user from accidentally including gtest-internal-inl.h in
  40. // his code.
  41. #define GTEST_IMPLEMENTATION_ 1
  42. #include "src/gtest-internal-inl.h" // For Vector.
  43. #undef GTEST_IMPLEMENTATION_
  44. using ::testing::AddGlobalTestEnvironment;
  45. using ::testing::Environment;
  46. using ::testing::InitGoogleTest;
  47. using ::testing::Test;
  48. using ::testing::TestCase;
  49. using ::testing::TestEventListener;
  50. using ::testing::TestInfo;
  51. using ::testing::TestPartResult;
  52. using ::testing::UnitTest;
  53. using ::testing::internal::String;
  54. using ::testing::internal::Vector;
  55. // Used by tests to register their events.
  56. Vector<String>* g_events = NULL;
  57. namespace testing {
  58. namespace internal {
  59. class EventRecordingListener : public TestEventListener {
  60. public:
  61. EventRecordingListener(const char* name) : name_(name) {}
  62. protected:
  63. virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
  64. g_events->PushBack(GetFullMethodName("OnTestProgramStart"));
  65. }
  66. virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
  67. int iteration) {
  68. Message message;
  69. message << GetFullMethodName("OnTestIterationStart")
  70. << "(" << iteration << ")";
  71. g_events->PushBack(message.GetString());
  72. }
  73. virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
  74. g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpStart"));
  75. }
  76. virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
  77. g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpEnd"));
  78. }
  79. virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
  80. g_events->PushBack(GetFullMethodName("OnTestCaseStart"));
  81. }
  82. virtual void OnTestStart(const TestInfo& /*test_info*/) {
  83. g_events->PushBack(GetFullMethodName("OnTestStart"));
  84. }
  85. virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
  86. g_events->PushBack(GetFullMethodName("OnTestPartResult"));
  87. }
  88. virtual void OnTestEnd(const TestInfo& /*test_info*/) {
  89. g_events->PushBack(GetFullMethodName("OnTestEnd"));
  90. }
  91. virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
  92. g_events->PushBack(GetFullMethodName("OnTestCaseEnd"));
  93. }
  94. virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
  95. g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownStart"));
  96. }
  97. virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
  98. g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownEnd"));
  99. }
  100. virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
  101. int iteration) {
  102. Message message;
  103. message << GetFullMethodName("OnTestIterationEnd")
  104. << "(" << iteration << ")";
  105. g_events->PushBack(message.GetString());
  106. }
  107. virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
  108. g_events->PushBack(GetFullMethodName("OnTestProgramEnd"));
  109. }
  110. private:
  111. String GetFullMethodName(const char* name) {
  112. Message message;
  113. message << name_ << "." << name;
  114. return message.GetString();
  115. }
  116. String name_;
  117. };
  118. class EnvironmentInvocationCatcher : public Environment {
  119. protected:
  120. virtual void SetUp() {
  121. g_events->PushBack(String("Environment::SetUp"));
  122. }
  123. virtual void TearDown() {
  124. g_events->PushBack(String("Environment::TearDown"));
  125. }
  126. };
  127. class ListenerTest : public Test {
  128. protected:
  129. static void SetUpTestCase() {
  130. g_events->PushBack(String("ListenerTest::SetUpTestCase"));
  131. }
  132. static void TearDownTestCase() {
  133. g_events->PushBack(String("ListenerTest::TearDownTestCase"));
  134. }
  135. virtual void SetUp() {
  136. g_events->PushBack(String("ListenerTest::SetUp"));
  137. }
  138. virtual void TearDown() {
  139. g_events->PushBack(String("ListenerTest::TearDown"));
  140. }
  141. };
  142. TEST_F(ListenerTest, DoesFoo) {
  143. // Test execution order within a test case is not guaranteed so we are not
  144. // recording the test name.
  145. g_events->PushBack(String("ListenerTest::* Test Body"));
  146. SUCCEED(); // Triggers OnTestPartResult.
  147. }
  148. TEST_F(ListenerTest, DoesBar) {
  149. g_events->PushBack(String("ListenerTest::* Test Body"));
  150. SUCCEED(); // Triggers OnTestPartResult.
  151. }
  152. } // namespace internal
  153. } // namespace testing
  154. using ::testing::internal::EnvironmentInvocationCatcher;
  155. using ::testing::internal::EventRecordingListener;
  156. void VerifyResults(const Vector<String>& data,
  157. const char* const* expected_data,
  158. int expected_data_size) {
  159. const int actual_size = data.size();
  160. // If the following assertion fails, a new entry will be appended to
  161. // data. Hence we save data.size() first.
  162. EXPECT_EQ(expected_data_size, actual_size);
  163. // Compares the common prefix.
  164. const int shorter_size = expected_data_size <= actual_size ?
  165. expected_data_size : actual_size;
  166. int i = 0;
  167. for (; i < shorter_size; ++i) {
  168. ASSERT_STREQ(expected_data[i], data.GetElement(i).c_str())
  169. << "at position " << i;
  170. }
  171. // Prints extra elements in the actual data.
  172. for (; i < actual_size; ++i) {
  173. printf(" Actual event #%d: %s\n", i, data.GetElement(i).c_str());
  174. }
  175. }
  176. int main(int argc, char **argv) {
  177. Vector<String> events;
  178. g_events = &events;
  179. InitGoogleTest(&argc, argv);
  180. UnitTest::GetInstance()->listeners().Append(
  181. new EventRecordingListener("1st"));
  182. UnitTest::GetInstance()->listeners().Append(
  183. new EventRecordingListener("2nd"));
  184. AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
  185. GTEST_CHECK_(events.size() == 0)
  186. << "AddGlobalTestEnvironment should not generate any events itself.";
  187. ::testing::GTEST_FLAG(repeat) = 2;
  188. int ret_val = RUN_ALL_TESTS();
  189. const char* const expected_events[] = {
  190. "1st.OnTestProgramStart",
  191. "2nd.OnTestProgramStart",
  192. "1st.OnTestIterationStart(0)",
  193. "2nd.OnTestIterationStart(0)",
  194. "1st.OnEnvironmentsSetUpStart",
  195. "2nd.OnEnvironmentsSetUpStart",
  196. "Environment::SetUp",
  197. "2nd.OnEnvironmentsSetUpEnd",
  198. "1st.OnEnvironmentsSetUpEnd",
  199. "1st.OnTestCaseStart",
  200. "2nd.OnTestCaseStart",
  201. "ListenerTest::SetUpTestCase",
  202. "1st.OnTestStart",
  203. "2nd.OnTestStart",
  204. "ListenerTest::SetUp",
  205. "ListenerTest::* Test Body",
  206. "1st.OnTestPartResult",
  207. "2nd.OnTestPartResult",
  208. "ListenerTest::TearDown",
  209. "2nd.OnTestEnd",
  210. "1st.OnTestEnd",
  211. "1st.OnTestStart",
  212. "2nd.OnTestStart",
  213. "ListenerTest::SetUp",
  214. "ListenerTest::* Test Body",
  215. "1st.OnTestPartResult",
  216. "2nd.OnTestPartResult",
  217. "ListenerTest::TearDown",
  218. "2nd.OnTestEnd",
  219. "1st.OnTestEnd",
  220. "ListenerTest::TearDownTestCase",
  221. "2nd.OnTestCaseEnd",
  222. "1st.OnTestCaseEnd",
  223. "1st.OnEnvironmentsTearDownStart",
  224. "2nd.OnEnvironmentsTearDownStart",
  225. "Environment::TearDown",
  226. "2nd.OnEnvironmentsTearDownEnd",
  227. "1st.OnEnvironmentsTearDownEnd",
  228. "2nd.OnTestIterationEnd(0)",
  229. "1st.OnTestIterationEnd(0)",
  230. "1st.OnTestIterationStart(1)",
  231. "2nd.OnTestIterationStart(1)",
  232. "1st.OnEnvironmentsSetUpStart",
  233. "2nd.OnEnvironmentsSetUpStart",
  234. "Environment::SetUp",
  235. "2nd.OnEnvironmentsSetUpEnd",
  236. "1st.OnEnvironmentsSetUpEnd",
  237. "1st.OnTestCaseStart",
  238. "2nd.OnTestCaseStart",
  239. "ListenerTest::SetUpTestCase",
  240. "1st.OnTestStart",
  241. "2nd.OnTestStart",
  242. "ListenerTest::SetUp",
  243. "ListenerTest::* Test Body",
  244. "1st.OnTestPartResult",
  245. "2nd.OnTestPartResult",
  246. "ListenerTest::TearDown",
  247. "2nd.OnTestEnd",
  248. "1st.OnTestEnd",
  249. "1st.OnTestStart",
  250. "2nd.OnTestStart",
  251. "ListenerTest::SetUp",
  252. "ListenerTest::* Test Body",
  253. "1st.OnTestPartResult",
  254. "2nd.OnTestPartResult",
  255. "ListenerTest::TearDown",
  256. "2nd.OnTestEnd",
  257. "1st.OnTestEnd",
  258. "ListenerTest::TearDownTestCase",
  259. "2nd.OnTestCaseEnd",
  260. "1st.OnTestCaseEnd",
  261. "1st.OnEnvironmentsTearDownStart",
  262. "2nd.OnEnvironmentsTearDownStart",
  263. "Environment::TearDown",
  264. "2nd.OnEnvironmentsTearDownEnd",
  265. "1st.OnEnvironmentsTearDownEnd",
  266. "2nd.OnTestIterationEnd(1)",
  267. "1st.OnTestIterationEnd(1)",
  268. "2nd.OnTestProgramEnd",
  269. "1st.OnTestProgramEnd"
  270. };
  271. VerifyResults(events,
  272. expected_events,
  273. sizeof(expected_events)/sizeof(expected_events[0]));
  274. // We need to check manually for ad hoc test failures that happen after
  275. // RUN_ALL_TESTS finishes.
  276. if (UnitTest::GetInstance()->Failed())
  277. ret_val = 1;
  278. return ret_val;
  279. }