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


fcl
Author(s):
autogenerated on Fri Apr 2 2021 02:38:00