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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:24