?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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.  
  31. // GOOGLETEST_CM0001 DO NOT DELETE
  32.  
  33. #ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
  34. #define GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
  35.  
  36. // This header implements typed tests and type-parameterized tests.
  37.  
  38. // Typed (aka type-driven) tests repeat the same test for types in a
  39. // list.  You must know which types you want to test with when writing
  40. // typed tests. Here's how you do it:
  41.  
  42. #if 0
  43.  
  44. // First, define a fixture class template.  It should be parameterized
  45. // by a type.  Remember to derive it from testing::Test.
  46. template <typename T>
  47. class FooTest : public testing::Test {
  48.  public:
  49.   ...
  50.   typedef std::list<T> List;
  51.   static T shared_;
  52.   T value_;
  53. };
  54.  
  55. // Next, associate a list of types with the test case, which will be
  56. // repeated for each type in the list.  The typedef is necessary for
  57. // the macro to parse correctly.
  58. typedef testing::Types<char, int, unsigned int> MyTypes;
  59. TYPED_TEST_CASE(FooTest, MyTypes);
  60.  
  61. // If the type list contains only one type, you can write that type
  62. // directly without Types<...>:
  63. //   TYPED_TEST_CASE(FooTest, int);
  64.  
  65. // Then, use TYPED_TEST() instead of TEST_F() to define as many typed
  66. // tests for this test case as you want.
  67. TYPED_TEST(FooTest, DoesBlah) {
  68.   // Inside a test, refer to TypeParam to get the type parameter.
  69.   // Since we are inside a derived class template, C++ requires use to
  70.   // visit the members of FooTest via 'this'.
  71.   TypeParam n = this->value_;
  72.  
  73.   // To visit static members of the fixture, add the TestFixture::
  74.   // prefix.
  75.   n += TestFixture::shared_;
  76.  
  77.   // To refer to typedefs in the fixture, add the "typename
  78.   // TestFixture::" prefix.
  79.   typename TestFixture::List values;
  80.   values.push_back(n);
  81.   ...
  82. }
  83.  
  84. TYPED_TEST(FooTest, HasPropertyA) { ... }
  85.  
  86. #endif  // 0
  87.  
  88. // Type-parameterized tests are abstract test patterns parameterized
  89. // by a type.  Compared with typed tests, type-parameterized tests
  90. // allow you to define the test pattern without knowing what the type
  91. // parameters are.  The defined pattern can be instantiated with
  92. // different types any number of times, in any number of translation
  93. // units.
  94. //
  95. // If you are designing an interface or concept, you can define a
  96. // suite of type-parameterized tests to verify properties that any
  97. // valid implementation of the interface/concept should have.  Then,
  98. // each implementation can easily instantiate the test suite to verify
  99. // that it conforms to the requirements, without having to write
  100. // similar tests repeatedly.  Here's an example:
  101.  
  102. #if 0
  103.  
  104. // First, define a fixture class template.  It should be parameterized
  105. // by a type.  Remember to derive it from testing::Test.
  106. template <typename T>
  107. class FooTest : public testing::Test {
  108.   ...
  109. };
  110.  
  111. // Next, declare that you will define a type-parameterized test case
  112. // (the _P suffix is for "parameterized" or "pattern", whichever you
  113. // prefer):
  114. TYPED_TEST_CASE_P(FooTest);
  115.  
  116. // Then, use TYPED_TEST_P() to define as many type-parameterized tests
  117. // for this type-parameterized test case as you want.
  118. TYPED_TEST_P(FooTest, DoesBlah) {
  119.   // Inside a test, refer to TypeParam to get the type parameter.
  120.   TypeParam n = 0;
  121.   ...
  122. }
  123.  
  124. TYPED_TEST_P(FooTest, HasPropertyA) { ... }
  125.  
  126. // Now the tricky part: you need to register all test patterns before
  127. // you can instantiate them.  The first argument of the macro is the
  128. // test case name; the rest are the names of the tests in this test
  129. // case.
  130. REGISTER_TYPED_TEST_CASE_P(FooTest,
  131.                            DoesBlah, HasPropertyA);
  132.  
  133. // Finally, you are free to instantiate the pattern with the types you
  134. // want.  If you put the above code in a header file, you can #include
  135. // it in multiple C++ source files and instantiate it multiple times.
  136. //
  137. // To distinguish different instances of the pattern, the first
  138. // argument to the INSTANTIATE_* macro is a prefix that will be added
  139. // to the actual test case name.  Remember to pick unique prefixes for
  140. // different instances.
  141. typedef testing::Types<char, int, unsigned int> MyTypes;
  142. INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);
  143.  
  144. // If the type list contains only one type, you can write that type
  145. // directly without Types<...>:
  146. //   INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int);
  147.  
  148. #endif  // 0
  149.  
  150. #include "gtest/internal/gtest-port.h"
  151. #include "gtest/internal/gtest-type-util.h"
  152.  
  153. // Implements typed tests.
  154.  
  155. #if GTEST_HAS_TYPED_TEST
  156.  
  157. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  158. //
  159. // Expands to the name of the typedef for the type parameters of the
  160. // given test case.
  161. # define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_
  162.  
  163. // The 'Types' template argument below must have spaces around it
  164. // since some compilers may choke on '>>' when passing a template
  165. // instance (e.g. Types<int>)
  166. # define TYPED_TEST_CASE(CaseName, Types) \
  167.   typedef ::testing::internal::TypeList< Types >::type \
  168.       GTEST_TYPE_PARAMS_(CaseName)
  169.  
  170. # define TYPED_TEST(CaseName, TestName) \
  171.   template <typename gtest_TypeParam_> \
  172.   class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
  173.       : public CaseName<gtest_TypeParam_> { \
  174.    private: \
  175.     typedef CaseName<gtest_TypeParam_> TestFixture; \
  176.     typedef gtest_TypeParam_ TypeParam; \
  177.     virtual void TestBody(); \
  178.   }; \
  179.   bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \
  180.       ::testing::internal::TypeParameterizedTest< \
  181.           CaseName, \
  182.           ::testing::internal::TemplateSel< \
  183.               GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \
  184.           GTEST_TYPE_PARAMS_(CaseName)>::Register(\
  185.               "", ::testing::internal::CodeLocation(__FILE__, __LINE__), \
  186.               #CaseName, #TestName, 0); \
  187.   template <typename gtest_TypeParam_> \
  188.   void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody()
  189.  
  190. #endif  // GTEST_HAS_TYPED_TEST
  191.  
  192. // Implements type-parameterized tests.
  193.  
  194. #if GTEST_HAS_TYPED_TEST_P
  195.  
  196. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  197. //
  198. // Expands to the namespace name that the type-parameterized tests for
  199. // the given type-parameterized test case are defined in.  The exact
  200. // name of the namespace is subject to change without notice.
  201. # define GTEST_CASE_NAMESPACE_(TestCaseName) \
  202.   gtest_case_##TestCaseName##_
  203.  
  204. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  205. //
  206. // Expands to the name of the variable used to remember the names of
  207. // the defined tests in the given test case.
  208. # define GTEST_TYPED_TEST_CASE_P_STATE_(TestCaseName) \
  209.   gtest_typed_test_case_p_state_##TestCaseName##_
  210.  
  211. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.
  212. //
  213. // Expands to the name of the variable used to remember the names of
  214. // the registered tests in the given test case.
  215. # define GTEST_REGISTERED_TEST_NAMES_(TestCaseName) \
  216.   gtest_registered_test_names_##TestCaseName##_
  217.  
  218. // The variables defined in the type-parameterized test macros are
  219. // static as typically these macros are used in a .h file that can be
  220. // #included in multiple translation units linked together.
  221. # define TYPED_TEST_CASE_P(CaseName) \
  222.   static ::testing::internal::TypedTestCasePState \
  223.       GTEST_TYPED_TEST_CASE_P_STATE_(CaseName)
  224.  
  225. # define TYPED_TEST_P(CaseName, TestName) \
  226.   namespace GTEST_CASE_NAMESPACE_(CaseName) { \
  227.   template <typename gtest_TypeParam_> \
  228.   class TestName : public CaseName<gtest_TypeParam_> { \
  229.    private: \
  230.     typedef CaseName<gtest_TypeParam_> TestFixture; \
  231.     typedef gtest_TypeParam_ TypeParam; \
  232.     virtual void TestBody(); \
  233.   }; \
  234.   static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \
  235.       GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).AddTestName(\
  236.           __FILE__, __LINE__, #CaseName, #TestName); \
  237.   } \
  238.   template <typename gtest_TypeParam_> \
  239.   void GTEST_CASE_NAMESPACE_(CaseName)::TestName<gtest_TypeParam_>::TestBody()
  240.  
  241. # define REGISTER_TYPED_TEST_CASE_P(CaseName, ...) \
  242.   namespace GTEST_CASE_NAMESPACE_(CaseName) { \
  243.   typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \
  244.   } \
  245.   static const char* const GTEST_REGISTERED_TEST_NAMES_(CaseName) \
  246.       GTEST_ATTRIBUTE_UNUSED_ = \
  247.           GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).VerifyRegisteredTestNames( \
  248.               __FILE__, __LINE__, #__VA_ARGS__)
  249.  
  250. // The 'Types' template argument below must have spaces around it
  251. // since some compilers may choke on '>>' when passing a template
  252. // instance (e.g. Types<int>)
  253. # define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \
  254.   bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \
  255.       ::testing::internal::TypeParameterizedTestCase<CaseName, \
  256.           GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \
  257.           ::testing::internal::TypeList< Types >::type>::Register(\
  258.               #Prefix, \
  259.               ::testing::internal::CodeLocation(__FILE__, __LINE__), \
  260.               &GTEST_TYPED_TEST_CASE_P_STATE_(CaseName), \
  261.               #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName))
  262.  
  263. #endif  // GTEST_HAS_TYPED_TEST_P
  264.  
  265. #endif  // GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
  266.