gtest-typed-test_test.cc
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 
32 
33 #include <set>
34 #include <vector>
35 
36 #include "gtest/gtest.h"
37 
38 #if _MSC_VER
39 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
40 #endif // _MSC_VER
41 
42 using testing::Test;
43 
44 // Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture
45 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
46 // type-parameterized test.
47 template <typename T>
48 class CommonTest : public Test {
49  // For some technical reason, SetUpTestSuite() and TearDownTestSuite()
50  // must be public.
51  public:
52  static void SetUpTestSuite() {
53  shared_ = new T(5);
54  }
55 
56  static void TearDownTestSuite() {
57  delete shared_;
58  shared_ = nullptr;
59  }
60 
61  // This 'protected:' is optional. There's no harm in making all
62  // members of this fixture class template public.
63  protected:
64  // We used to use std::list here, but switched to std::vector since
65  // MSVC's <list> doesn't compile cleanly with /W4.
66  typedef std::vector<T> Vector;
67  typedef std::set<int> IntSet;
68 
69  CommonTest() : value_(1) {}
70 
71  ~CommonTest() override { EXPECT_EQ(3, value_); }
72 
73  void SetUp() override {
74  EXPECT_EQ(1, value_);
75  value_++;
76  }
77 
78  void TearDown() override {
79  EXPECT_EQ(2, value_);
80  value_++;
81  }
82 
84  static T* shared_;
85 };
86 
87 template <typename T>
88 T* CommonTest<T>::shared_ = nullptr;
89 
90 // This #ifdef block tests typed tests.
91 #if GTEST_HAS_TYPED_TEST
92 
93 using testing::Types;
94 
95 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
96 // and SetUp()/TearDown() work correctly in typed tests
97 
98 typedef Types<char, int> TwoTypes;
99 TYPED_TEST_SUITE(CommonTest, TwoTypes);
100 
101 TYPED_TEST(CommonTest, ValuesAreCorrect) {
102  // Static members of the fixture class template can be visited via
103  // the TestFixture:: prefix.
104  EXPECT_EQ(5, *TestFixture::shared_);
105 
106  // Typedefs in the fixture class template can be visited via the
107  // "typename TestFixture::" prefix.
108  typename TestFixture::Vector empty;
109  EXPECT_EQ(0U, empty.size());
110 
111  typename TestFixture::IntSet empty2;
112  EXPECT_EQ(0U, empty2.size());
113 
114  // Non-static members of the fixture class must be visited via
115  // 'this', as required by C++ for class templates.
116  EXPECT_EQ(2, this->value_);
117 }
118 
119 // The second test makes sure shared_ is not deleted after the first
120 // test.
121 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
122  // Static members of the fixture class template can also be visited
123  // via 'this'.
124  ASSERT_TRUE(this->shared_ != nullptr);
125  EXPECT_EQ(5, *this->shared_);
126 
127  // TypeParam can be used to refer to the type parameter.
128  EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
129 }
130 
131 // Tests that multiple TYPED_TEST_SUITE's can be defined in the same
132 // translation unit.
133 
134 template <typename T>
135 class TypedTest1 : public Test {
136 };
137 
138 // Verifies that the second argument of TYPED_TEST_SUITE can be a
139 // single type.
140 TYPED_TEST_SUITE(TypedTest1, int);
141 TYPED_TEST(TypedTest1, A) {}
142 
143 template <typename T>
144 class TypedTest2 : public Test {
145 };
146 
147 // Verifies that the second argument of TYPED_TEST_SUITE can be a
148 // Types<...> type list.
149 TYPED_TEST_SUITE(TypedTest2, Types<int>);
150 
151 // This also verifies that tests from different typed test cases can
152 // share the same name.
153 TYPED_TEST(TypedTest2, A) {}
154 
155 // Tests that a typed test case can be defined in a namespace.
156 
157 namespace library1 {
158 
159 template <typename T>
160 class NumericTest : public Test {
161 };
162 
163 typedef Types<int, long> NumericTypes;
164 TYPED_TEST_SUITE(NumericTest, NumericTypes);
165 
166 TYPED_TEST(NumericTest, DefaultIsZero) {
167  EXPECT_EQ(0, TypeParam());
168 }
169 
170 } // namespace library1
171 
172 // Tests that custom names work.
173 template <typename T>
174 class TypedTestWithNames : public Test {};
175 
176 class TypedTestNames {
177  public:
178  template <typename T>
179  static std::string GetName(int i) {
181  return std::string("char") + ::testing::PrintToString(i);
182  }
184  return std::string("int") + ::testing::PrintToString(i);
185  }
186  }
187 };
188 
189 TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
190 
191 TYPED_TEST(TypedTestWithNames, TestSuiteName) {
194  ->current_test_info()
195  ->test_case_name(),
196  "TypedTestWithNames/char0");
197  }
200  ->current_test_info()
201  ->test_case_name(),
202  "TypedTestWithNames/int1");
203  }
204 }
205 
206 #endif // GTEST_HAS_TYPED_TEST
207 
208 // This #ifdef block tests type-parameterized tests.
209 #if GTEST_HAS_TYPED_TEST_P
210 
211 using testing::Types;
212 using testing::internal::TypedTestSuitePState;
213 
214 // Tests TypedTestSuitePState.
215 
216 class TypedTestSuitePStateTest : public Test {
217  protected:
218  void SetUp() override {
219  state_.AddTestName("foo.cc", 0, "FooTest", "A");
220  state_.AddTestName("foo.cc", 0, "FooTest", "B");
221  state_.AddTestName("foo.cc", 0, "FooTest", "C");
222  }
223 
224  TypedTestSuitePState state_;
225 };
226 
227 TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
228  const char* tests = "A, B, C";
230  state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
231 }
232 
233 // Makes sure that the order of the tests and spaces around the names
234 // don't matter.
235 TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
236  const char* tests = "A,C, B";
238  state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
239 }
240 
241 using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
242 
243 TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
245  state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
246  "foo\\.cc.1.?: Test A is listed more than once\\.");
247 }
248 
249 TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
251  state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
252  "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
253 }
254 
255 TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
257  state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
258  "foo\\.cc.1.?: You forgot to list test B\\.");
259 }
260 
261 // Tests that defining a test for a parameterized test case generates
262 // a run-time error if the test case has been registered.
263 TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
264  state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
266  state_.AddTestName("foo.cc", 2, "FooTest", "D"),
267  "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
268  "\\(FooTest, \\.\\.\\.\\)\\.");
269 }
270 
271 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
272 // and SetUp()/TearDown() work correctly in type-parameterized tests.
273 
274 template <typename T>
275 class DerivedTest : public CommonTest<T> {
276 };
277 
278 TYPED_TEST_SUITE_P(DerivedTest);
279 
280 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
281  // Static members of the fixture class template can be visited via
282  // the TestFixture:: prefix.
283  EXPECT_EQ(5, *TestFixture::shared_);
284 
285  // Non-static members of the fixture class must be visited via
286  // 'this', as required by C++ for class templates.
287  EXPECT_EQ(2, this->value_);
288 }
289 
290 // The second test makes sure shared_ is not deleted after the first
291 // test.
292 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
293  // Static members of the fixture class template can also be visited
294  // via 'this'.
295  ASSERT_TRUE(this->shared_ != nullptr);
296  EXPECT_EQ(5, *this->shared_);
297  EXPECT_EQ(2, this->value_);
298 }
299 
300 REGISTER_TYPED_TEST_SUITE_P(DerivedTest,
301  ValuesAreCorrect, ValuesAreStillCorrect);
302 
303 typedef Types<short, long> MyTwoTypes;
304 INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
305 
306 // Tests that custom names work with type parametrized tests. We reuse the
307 // TwoTypes from above here.
308 template <typename T>
309 class TypeParametrizedTestWithNames : public Test {};
310 
311 TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
312 
313 TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
316  ->current_test_info()
317  ->test_case_name(),
318  "CustomName/TypeParametrizedTestWithNames/parChar0");
319  }
322  ->current_test_info()
323  ->test_case_name(),
324  "CustomName/TypeParametrizedTestWithNames/parInt1");
325  }
326 }
327 
328 REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
329 
330 class TypeParametrizedTestNames {
331  public:
332  template <typename T>
333  static std::string GetName(int i) {
335  return std::string("parChar") + ::testing::PrintToString(i);
336  }
338  return std::string("parInt") + ::testing::PrintToString(i);
339  }
340  }
341 };
342 
343 INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames,
344  TwoTypes, TypeParametrizedTestNames);
345 
346 // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
347 // translation unit.
348 
349 template <typename T>
350 class TypedTestP1 : public Test {
351 };
352 
353 TYPED_TEST_SUITE_P(TypedTestP1);
354 
355 // For testing that the code between TYPED_TEST_SUITE_P() and
356 // TYPED_TEST_P() is not enclosed in a namespace.
357 using IntAfterTypedTestSuiteP = int;
358 
359 TYPED_TEST_P(TypedTestP1, A) {}
360 TYPED_TEST_P(TypedTestP1, B) {}
361 
362 // For testing that the code between TYPED_TEST_P() and
363 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
364 using IntBeforeRegisterTypedTestSuiteP = int;
365 
366 REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
367 
368 template <typename T>
369 class TypedTestP2 : public Test {
370 };
371 
372 TYPED_TEST_SUITE_P(TypedTestP2);
373 
374 // This also verifies that tests from different type-parameterized
375 // test cases can share the same name.
376 TYPED_TEST_P(TypedTestP2, A) {}
377 
378 REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
379 
380 // Verifies that the code between TYPED_TEST_SUITE_P() and
381 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
382 IntAfterTypedTestSuiteP after = 0;
383 IntBeforeRegisterTypedTestSuiteP before = 0;
384 
385 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
386 // can be either a single type or a Types<...> type list.
387 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
388 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP2, Types<int>);
389 
390 // Tests that the same type-parameterized test case can be
391 // instantiated more than once in the same translation unit.
392 INSTANTIATE_TYPED_TEST_SUITE_P(Double, TypedTestP2, Types<double>);
393 
394 // Tests that the same type-parameterized test case can be
395 // instantiated in different translation units linked together.
396 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
397 typedef Types<std::vector<double>, std::set<char> > MyContainers;
398 INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers);
399 
400 // Tests that a type-parameterized test case can be defined and
401 // instantiated in a namespace.
402 
403 namespace library2 {
404 
405 template <typename T>
406 class NumericTest : public Test {
407 };
408 
409 TYPED_TEST_SUITE_P(NumericTest);
410 
411 TYPED_TEST_P(NumericTest, DefaultIsZero) {
412  EXPECT_EQ(0, TypeParam());
413 }
414 
415 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
416  EXPECT_LT(TypeParam(0), TypeParam(1));
417 }
418 
419 REGISTER_TYPED_TEST_SUITE_P(NumericTest,
420  DefaultIsZero, ZeroIsLessThanOne);
421 typedef Types<int, double> NumericTypes;
422 INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
423 
424 static const char* GetTestName() {
426 }
427 // Test the stripping of space from test names
428 template <typename T> class TrimmedTest : public Test { };
429 TYPED_TEST_SUITE_P(TrimmedTest);
430 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
431 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
432 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
433 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
434 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
436  TrimmedTest,
437  Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
438 template <typename T1, typename T2> struct MyPair {};
439 // Be sure to try a type with a comma in its name just in case it matters.
440 typedef Types<int, double, MyPair<int, int> > TrimTypes;
441 INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes);
442 
443 } // namespace library2
444 
445 #endif // GTEST_HAS_TYPED_TEST_P
446 
447 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
448 
449 // Google Test may not support type-parameterized tests with some
450 // compilers. If we use conditional compilation to compile out all
451 // code referring to the gtest_main library, MSVC linker will not link
452 // that library at all and consequently complain about missing entry
453 // point defined in that library (fatal error LNK1561: entry point
454 // must be defined). This dummy test keeps gtest_main linked in.
455 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
456 
457 #if _MSC_VER
459 #endif // _MSC_VER
460 
461 #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
testing::Test::SetUp
virtual void SetUp()
Definition: gtest.cc:2249
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2398
REGISTER_TYPED_TEST_SUITE_P
REGISTER_TYPED_TEST_SUITE_P(TypeParamTest, TestA, TestB)
testing::UnitTest::current_test_info
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
Definition: gtest.cc:4874
CommonTest::SetUpTestSuite
static void SetUpTestSuite()
Definition: gtest-typed-test_test.cc:52
CommonTest::TearDown
void TearDown() override
Definition: gtest-typed-test_test.cc:78
gtest.h
testing::internal::IsSame
Definition: gtest-port.h:867
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
CommonTest::shared_
static T * shared_
Definition: gtest-typed-test_test.cc:84
TYPED_TEST_SUITE_P
TYPED_TEST_SUITE_P(TypeParamTest)
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
gtest-typed-test_test.h
CommonTest::CommonTest
CommonTest()
Definition: gtest-typed-test_test.cc:69
T
#define T(upbtypeconst, upbtype, ctype, default_value)
testing::Test
Definition: gtest.h:415
testing::internal::Double
FloatingPoint< double > Double
Definition: gtest-internal.h:429
GTEST_DISABLE_MSC_WARNINGS_PUSH_
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:311
CommonTest::SetUp
void SetUp() override
Definition: gtest-typed-test_test.cc:73
A
Definition: logging_striptest_main.cc:56
EXPECT_STREQ
#define EXPECT_STREQ(val1, val2)
Definition: glog/src/googletest.h:184
TYPED_TEST
TYPED_TEST(TypedTest, TestA)
Definition: googletest-list-tests-unittest_.cc:128
EXPECT_DEATH_IF_SUPPORTED
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Definition: gtest-death-test.h:335
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: gtest.h:1995
Json::Int
int Int
Definition: json.h:228
INSTANTIATE_TYPED_TEST_SUITE_P
INSTANTIATE_TYPED_TEST_SUITE_P(My, TypeParamTest, MyTypes)
CommonTest::IntSet
std::set< int > IntSet
Definition: gtest-typed-test_test.cc:67
TYPED_TEST_SUITE
TYPED_TEST_SUITE(TypedTest, MyTypes)
i
int i
Definition: gmock-matchers_test.cc:764
CommonTest::value_
T value_
Definition: gtest-typed-test_test.cc:83
value_
int value_
Definition: gmock-matchers_test.cc:571
testing::TestInfo::name
const char * name() const
Definition: gtest.h:710
tests
Definition: python/compatibility_tests/v2.5.0/tests/__init__.py:1
CommonTest::TearDownTestSuite
static void TearDownTestSuite()
Definition: gtest-typed-test_test.cc:56
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: glog/src/googletest.h:158
CommonTest::~CommonTest
~CommonTest() override
Definition: gtest-typed-test_test.cc:71
testing::UnitTest::GetInstance
static UnitTest * GetInstance()
Definition: gtest.cc:4538
google::protobuf::python::message_descriptor::GetName
static PyObject * GetName(PyBaseDescriptor *self, void *closure)
Definition: python/google/protobuf/pyext/descriptor.cc:471
TYPED_TEST_P
TYPED_TEST_P(TypeParamTest, TestA)
Definition: googletest-list-tests-unittest_.cc:142
CommonTest
Definition: gtest-typed-test_test.cc:48
TEST
TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform)
Definition: gtest-typed-test_test.cc:455
CommonTest::Vector
std::vector< T > Vector
Definition: gtest-typed-test_test.cc:66
GTEST_DISABLE_MSC_WARNINGS_POP_
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:312
testing::PrintToString
::std::string PrintToString(const T &value)
Definition: gtest-printers.h:938


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:53