/Unittests/googletest/test/gtest-typed-test_test.cc

http://unladen-swallow.googlecode.com/ · C++ · 360 lines · 175 code · 82 blank · 103 comment · 3 complexity · 9dbd84a837b777b7ca0852dcb1c42a69 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. // All Rights Reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. #include <set>
  32. #include <vector>
  33. #include "test/gtest-typed-test_test.h"
  34. #include <gtest/gtest.h>
  35. using testing::Test;
  36. // Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
  37. // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
  38. // type-parameterized test.
  39. template <typename T>
  40. class CommonTest : public Test {
  41. // For some technical reason, SetUpTestCase() and TearDownTestCase()
  42. // must be public.
  43. public:
  44. static void SetUpTestCase() {
  45. shared_ = new T(5);
  46. }
  47. static void TearDownTestCase() {
  48. delete shared_;
  49. shared_ = NULL;
  50. }
  51. // This 'protected:' is optional. There's no harm in making all
  52. // members of this fixture class template public.
  53. protected:
  54. // We used to use std::list here, but switched to std::vector since
  55. // MSVC's <list> doesn't compile cleanly with /W4.
  56. typedef std::vector<T> Vector;
  57. typedef std::set<int> IntSet;
  58. CommonTest() : value_(1) {}
  59. virtual ~CommonTest() { EXPECT_EQ(3, value_); }
  60. virtual void SetUp() {
  61. EXPECT_EQ(1, value_);
  62. value_++;
  63. }
  64. virtual void TearDown() {
  65. EXPECT_EQ(2, value_);
  66. value_++;
  67. }
  68. T value_;
  69. static T* shared_;
  70. };
  71. template <typename T>
  72. T* CommonTest<T>::shared_ = NULL;
  73. // This #ifdef block tests typed tests.
  74. #if GTEST_HAS_TYPED_TEST
  75. using testing::Types;
  76. // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
  77. // and SetUp()/TearDown() work correctly in typed tests
  78. typedef Types<char, int> TwoTypes;
  79. TYPED_TEST_CASE(CommonTest, TwoTypes);
  80. TYPED_TEST(CommonTest, ValuesAreCorrect) {
  81. // Static members of the fixture class template can be visited via
  82. // the TestFixture:: prefix.
  83. EXPECT_EQ(5, *TestFixture::shared_);
  84. // Typedefs in the fixture class template can be visited via the
  85. // "typename TestFixture::" prefix.
  86. typename TestFixture::Vector empty;
  87. EXPECT_EQ(0U, empty.size());
  88. typename TestFixture::IntSet empty2;
  89. EXPECT_EQ(0U, empty2.size());
  90. // Non-static members of the fixture class must be visited via
  91. // 'this', as required by C++ for class templates.
  92. EXPECT_EQ(2, this->value_);
  93. }
  94. // The second test makes sure shared_ is not deleted after the first
  95. // test.
  96. TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
  97. // Static members of the fixture class template can also be visited
  98. // via 'this'.
  99. ASSERT_TRUE(this->shared_ != NULL);
  100. EXPECT_EQ(5, *this->shared_);
  101. // TypeParam can be used to refer to the type parameter.
  102. EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
  103. }
  104. // Tests that multiple TYPED_TEST_CASE's can be defined in the same
  105. // translation unit.
  106. template <typename T>
  107. class TypedTest1 : public Test {
  108. };
  109. // Verifies that the second argument of TYPED_TEST_CASE can be a
  110. // single type.
  111. TYPED_TEST_CASE(TypedTest1, int);
  112. TYPED_TEST(TypedTest1, A) {}
  113. template <typename T>
  114. class TypedTest2 : public Test {
  115. };
  116. // Verifies that the second argument of TYPED_TEST_CASE can be a
  117. // Types<...> type list.
  118. TYPED_TEST_CASE(TypedTest2, Types<int>);
  119. // This also verifies that tests from different typed test cases can
  120. // share the same name.
  121. TYPED_TEST(TypedTest2, A) {}
  122. // Tests that a typed test case can be defined in a namespace.
  123. namespace library1 {
  124. template <typename T>
  125. class NumericTest : public Test {
  126. };
  127. typedef Types<int, long> NumericTypes;
  128. TYPED_TEST_CASE(NumericTest, NumericTypes);
  129. TYPED_TEST(NumericTest, DefaultIsZero) {
  130. EXPECT_EQ(0, TypeParam());
  131. }
  132. } // namespace library1
  133. #endif // GTEST_HAS_TYPED_TEST
  134. // This #ifdef block tests type-parameterized tests.
  135. #if GTEST_HAS_TYPED_TEST_P
  136. using testing::Types;
  137. using testing::internal::TypedTestCasePState;
  138. // Tests TypedTestCasePState.
  139. class TypedTestCasePStateTest : public Test {
  140. protected:
  141. virtual void SetUp() {
  142. state_.AddTestName("foo.cc", 0, "FooTest", "A");
  143. state_.AddTestName("foo.cc", 0, "FooTest", "B");
  144. state_.AddTestName("foo.cc", 0, "FooTest", "C");
  145. }
  146. TypedTestCasePState state_;
  147. };
  148. TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
  149. const char* tests = "A, B, C";
  150. EXPECT_EQ(tests,
  151. state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
  152. }
  153. // Makes sure that the order of the tests and spaces around the names
  154. // don't matter.
  155. TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
  156. const char* tests = "A,C, B";
  157. EXPECT_EQ(tests,
  158. state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
  159. }
  160. typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
  161. TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
  162. EXPECT_DEATH_IF_SUPPORTED(
  163. state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
  164. "foo\\.cc.1.?: Test A is listed more than once\\.");
  165. }
  166. TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
  167. EXPECT_DEATH_IF_SUPPORTED(
  168. state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
  169. "foo\\.cc.1.?: No test named D can be found in this test case\\.");
  170. }
  171. TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
  172. EXPECT_DEATH_IF_SUPPORTED(
  173. state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
  174. "foo\\.cc.1.?: You forgot to list test B\\.");
  175. }
  176. // Tests that defining a test for a parameterized test case generates
  177. // a run-time error if the test case has been registered.
  178. TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
  179. state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
  180. EXPECT_DEATH_IF_SUPPORTED(
  181. state_.AddTestName("foo.cc", 2, "FooTest", "D"),
  182. "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
  183. "\\(FooTest, \\.\\.\\.\\)\\.");
  184. }
  185. // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
  186. // and SetUp()/TearDown() work correctly in type-parameterized tests.
  187. template <typename T>
  188. class DerivedTest : public CommonTest<T> {
  189. };
  190. TYPED_TEST_CASE_P(DerivedTest);
  191. TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
  192. // Static members of the fixture class template can be visited via
  193. // the TestFixture:: prefix.
  194. EXPECT_EQ(5, *TestFixture::shared_);
  195. // Non-static members of the fixture class must be visited via
  196. // 'this', as required by C++ for class templates.
  197. EXPECT_EQ(2, this->value_);
  198. }
  199. // The second test makes sure shared_ is not deleted after the first
  200. // test.
  201. TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
  202. // Static members of the fixture class template can also be visited
  203. // via 'this'.
  204. ASSERT_TRUE(this->shared_ != NULL);
  205. EXPECT_EQ(5, *this->shared_);
  206. EXPECT_EQ(2, this->value_);
  207. }
  208. REGISTER_TYPED_TEST_CASE_P(DerivedTest,
  209. ValuesAreCorrect, ValuesAreStillCorrect);
  210. typedef Types<short, long> MyTwoTypes;
  211. INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
  212. // Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
  213. // translation unit.
  214. template <typename T>
  215. class TypedTestP1 : public Test {
  216. };
  217. TYPED_TEST_CASE_P(TypedTestP1);
  218. // For testing that the code between TYPED_TEST_CASE_P() and
  219. // TYPED_TEST_P() is not enclosed in a namespace.
  220. typedef int IntAfterTypedTestCaseP;
  221. TYPED_TEST_P(TypedTestP1, A) {}
  222. TYPED_TEST_P(TypedTestP1, B) {}
  223. // For testing that the code between TYPED_TEST_P() and
  224. // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
  225. typedef int IntBeforeRegisterTypedTestCaseP;
  226. REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
  227. template <typename T>
  228. class TypedTestP2 : public Test {
  229. };
  230. TYPED_TEST_CASE_P(TypedTestP2);
  231. // This also verifies that tests from different type-parameterized
  232. // test cases can share the same name.
  233. TYPED_TEST_P(TypedTestP2, A) {}
  234. REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
  235. // Verifies that the code between TYPED_TEST_CASE_P() and
  236. // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
  237. IntAfterTypedTestCaseP after = 0;
  238. IntBeforeRegisterTypedTestCaseP before = 0;
  239. // Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
  240. // can be either a single type or a Types<...> type list.
  241. INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
  242. INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
  243. // Tests that the same type-parameterized test case can be
  244. // instantiated more than once in the same translation unit.
  245. INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
  246. // Tests that the same type-parameterized test case can be
  247. // instantiated in different translation units linked together.
  248. // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
  249. typedef Types<std::vector<double>, std::set<char> > MyContainers;
  250. INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
  251. // Tests that a type-parameterized test case can be defined and
  252. // instantiated in a namespace.
  253. namespace library2 {
  254. template <typename T>
  255. class NumericTest : public Test {
  256. };
  257. TYPED_TEST_CASE_P(NumericTest);
  258. TYPED_TEST_P(NumericTest, DefaultIsZero) {
  259. EXPECT_EQ(0, TypeParam());
  260. }
  261. TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
  262. EXPECT_LT(TypeParam(0), TypeParam(1));
  263. }
  264. REGISTER_TYPED_TEST_CASE_P(NumericTest,
  265. DefaultIsZero, ZeroIsLessThanOne);
  266. typedef Types<int, double> NumericTypes;
  267. INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
  268. } // namespace library2
  269. #endif // GTEST_HAS_TYPED_TEST_P
  270. #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
  271. // Google Test doesn't support type-parameterized tests on some platforms
  272. // and compilers, such as MSVC 7.1. If we use conditional compilation to
  273. // compile out all code referring to the gtest_main library, MSVC linker
  274. // will not link that library at all and consequently complain about
  275. // missing entry point defined in that library (fatal error LNK1561:
  276. // entry point must be defined). This dummy test keeps gtest_main linked in.
  277. TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
  278. #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)