Imported Upstream version 1.12.0
[platform/upstream/gtest.git] / googletest / test / gtest-typed-test_test.cc
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 #include "test/gtest-typed-test_test.h"
31
32 #include <set>
33 #include <type_traits>
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() { shared_ = new T(5); }
53
54   static void TearDownTestSuite() {
55     delete shared_;
56     shared_ = nullptr;
57   }
58
59   // This 'protected:' is optional.  There's no harm in making all
60   // members of this fixture class template public.
61  protected:
62   // We used to use std::list here, but switched to std::vector since
63   // MSVC's <list> doesn't compile cleanly with /W4.
64   typedef std::vector<T> Vector;
65   typedef std::set<int> IntSet;
66
67   CommonTest() : value_(1) {}
68
69   ~CommonTest() override { EXPECT_EQ(3, value_); }
70
71   void SetUp() override {
72     EXPECT_EQ(1, value_);
73     value_++;
74   }
75
76   void TearDown() override {
77     EXPECT_EQ(2, value_);
78     value_++;
79   }
80
81   T value_;
82   static T* shared_;
83 };
84
85 template <typename T>
86 T* CommonTest<T>::shared_ = nullptr;
87
88 using testing::Types;
89
90 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
91 // and SetUp()/TearDown() work correctly in typed tests
92
93 typedef Types<char, int> TwoTypes;
94 TYPED_TEST_SUITE(CommonTest, TwoTypes);
95
96 TYPED_TEST(CommonTest, ValuesAreCorrect) {
97   // Static members of the fixture class template can be visited via
98   // the TestFixture:: prefix.
99   EXPECT_EQ(5, *TestFixture::shared_);
100
101   // Typedefs in the fixture class template can be visited via the
102   // "typename TestFixture::" prefix.
103   typename TestFixture::Vector empty;
104   EXPECT_EQ(0U, empty.size());
105
106   typename TestFixture::IntSet empty2;
107   EXPECT_EQ(0U, empty2.size());
108
109   // Non-static members of the fixture class must be visited via
110   // 'this', as required by C++ for class templates.
111   EXPECT_EQ(2, this->value_);
112 }
113
114 // The second test makes sure shared_ is not deleted after the first
115 // test.
116 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
117   // Static members of the fixture class template can also be visited
118   // via 'this'.
119   ASSERT_TRUE(this->shared_ != nullptr);
120   EXPECT_EQ(5, *this->shared_);
121
122   // TypeParam can be used to refer to the type parameter.
123   EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
124 }
125
126 // Tests that multiple TYPED_TEST_SUITE's can be defined in the same
127 // translation unit.
128
129 template <typename T>
130 class TypedTest1 : public Test {};
131
132 // Verifies that the second argument of TYPED_TEST_SUITE can be a
133 // single type.
134 TYPED_TEST_SUITE(TypedTest1, int);
135 TYPED_TEST(TypedTest1, A) {}
136
137 template <typename T>
138 class TypedTest2 : public Test {};
139
140 // Verifies that the second argument of TYPED_TEST_SUITE can be a
141 // Types<...> type list.
142 TYPED_TEST_SUITE(TypedTest2, Types<int>);
143
144 // This also verifies that tests from different typed test cases can
145 // share the same name.
146 TYPED_TEST(TypedTest2, A) {}
147
148 // Tests that a typed test case can be defined in a namespace.
149
150 namespace library1 {
151
152 template <typename T>
153 class NumericTest : public Test {};
154
155 typedef Types<int, long> NumericTypes;
156 TYPED_TEST_SUITE(NumericTest, NumericTypes);
157
158 TYPED_TEST(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); }
159
160 }  // namespace library1
161
162 // Tests that custom names work.
163 template <typename T>
164 class TypedTestWithNames : public Test {};
165
166 class TypedTestNames {
167  public:
168   template <typename T>
169   static std::string GetName(int i) {
170     if (std::is_same<T, char>::value) {
171       return std::string("char") + ::testing::PrintToString(i);
172     }
173     if (std::is_same<T, int>::value) {
174       return std::string("int") + ::testing::PrintToString(i);
175     }
176   }
177 };
178
179 TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
180
181 TYPED_TEST(TypedTestWithNames, TestSuiteName) {
182   if (std::is_same<TypeParam, char>::value) {
183     EXPECT_STREQ(::testing::UnitTest::GetInstance()
184                      ->current_test_info()
185                      ->test_suite_name(),
186                  "TypedTestWithNames/char0");
187   }
188   if (std::is_same<TypeParam, int>::value) {
189     EXPECT_STREQ(::testing::UnitTest::GetInstance()
190                      ->current_test_info()
191                      ->test_suite_name(),
192                  "TypedTestWithNames/int1");
193   }
194 }
195
196 using testing::Types;
197 using testing::internal::TypedTestSuitePState;
198
199 // Tests TypedTestSuitePState.
200
201 class TypedTestSuitePStateTest : public Test {
202  protected:
203   void SetUp() override {
204     state_.AddTestName("foo.cc", 0, "FooTest", "A");
205     state_.AddTestName("foo.cc", 0, "FooTest", "B");
206     state_.AddTestName("foo.cc", 0, "FooTest", "C");
207   }
208
209   TypedTestSuitePState state_;
210 };
211
212 TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
213   const char* tests = "A, B, C";
214   EXPECT_EQ(tests,
215             state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
216 }
217
218 // Makes sure that the order of the tests and spaces around the names
219 // don't matter.
220 TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
221   const char* tests = "A,C,   B";
222   EXPECT_EQ(tests,
223             state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
224 }
225
226 using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
227
228 TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
229   EXPECT_DEATH_IF_SUPPORTED(
230       state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
231       "foo\\.cc.1.?: Test A is listed more than once\\.");
232 }
233
234 TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
235   EXPECT_DEATH_IF_SUPPORTED(
236       state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C, D"),
237       "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
238 }
239
240 TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
241   EXPECT_DEATH_IF_SUPPORTED(
242       state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, C"),
243       "foo\\.cc.1.?: You forgot to list test B\\.");
244 }
245
246 // Tests that defining a test for a parameterized test case generates
247 // a run-time error if the test case has been registered.
248 TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
249   state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
250   EXPECT_DEATH_IF_SUPPORTED(
251       state_.AddTestName("foo.cc", 2, "FooTest", "D"),
252       "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
253       "\\(FooTest, \\.\\.\\.\\)\\.");
254 }
255
256 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
257 // and SetUp()/TearDown() work correctly in type-parameterized tests.
258
259 template <typename T>
260 class DerivedTest : public CommonTest<T> {};
261
262 TYPED_TEST_SUITE_P(DerivedTest);
263
264 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
265   // Static members of the fixture class template can be visited via
266   // the TestFixture:: prefix.
267   EXPECT_EQ(5, *TestFixture::shared_);
268
269   // Non-static members of the fixture class must be visited via
270   // 'this', as required by C++ for class templates.
271   EXPECT_EQ(2, this->value_);
272 }
273
274 // The second test makes sure shared_ is not deleted after the first
275 // test.
276 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
277   // Static members of the fixture class template can also be visited
278   // via 'this'.
279   ASSERT_TRUE(this->shared_ != nullptr);
280   EXPECT_EQ(5, *this->shared_);
281   EXPECT_EQ(2, this->value_);
282 }
283
284 REGISTER_TYPED_TEST_SUITE_P(DerivedTest, ValuesAreCorrect,
285                             ValuesAreStillCorrect);
286
287 typedef Types<short, long> MyTwoTypes;
288 INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
289
290 // Tests that custom names work with type parametrized tests. We reuse the
291 // TwoTypes from above here.
292 template <typename T>
293 class TypeParametrizedTestWithNames : public Test {};
294
295 TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
296
297 TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
298   if (std::is_same<TypeParam, char>::value) {
299     EXPECT_STREQ(::testing::UnitTest::GetInstance()
300                      ->current_test_info()
301                      ->test_suite_name(),
302                  "CustomName/TypeParametrizedTestWithNames/parChar0");
303   }
304   if (std::is_same<TypeParam, int>::value) {
305     EXPECT_STREQ(::testing::UnitTest::GetInstance()
306                      ->current_test_info()
307                      ->test_suite_name(),
308                  "CustomName/TypeParametrizedTestWithNames/parInt1");
309   }
310 }
311
312 REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
313
314 class TypeParametrizedTestNames {
315  public:
316   template <typename T>
317   static std::string GetName(int i) {
318     if (std::is_same<T, char>::value) {
319       return std::string("parChar") + ::testing::PrintToString(i);
320     }
321     if (std::is_same<T, int>::value) {
322       return std::string("parInt") + ::testing::PrintToString(i);
323     }
324   }
325 };
326
327 INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames,
328                                TwoTypes, TypeParametrizedTestNames);
329
330 // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
331 // translation unit.
332
333 template <typename T>
334 class TypedTestP1 : public Test {};
335
336 TYPED_TEST_SUITE_P(TypedTestP1);
337
338 // For testing that the code between TYPED_TEST_SUITE_P() and
339 // TYPED_TEST_P() is not enclosed in a namespace.
340 using IntAfterTypedTestSuiteP = int;
341
342 TYPED_TEST_P(TypedTestP1, A) {}
343 TYPED_TEST_P(TypedTestP1, B) {}
344
345 // For testing that the code between TYPED_TEST_P() and
346 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
347 using IntBeforeRegisterTypedTestSuiteP = int;
348
349 REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
350
351 template <typename T>
352 class TypedTestP2 : public Test {};
353
354 TYPED_TEST_SUITE_P(TypedTestP2);
355
356 // This also verifies that tests from different type-parameterized
357 // test cases can share the same name.
358 TYPED_TEST_P(TypedTestP2, A) {}
359
360 REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
361
362 // Verifies that the code between TYPED_TEST_SUITE_P() and
363 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
364 IntAfterTypedTestSuiteP after = 0;
365 IntBeforeRegisterTypedTestSuiteP before = 0;
366
367 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
368 // can be either a single type or a Types<...> type list.
369 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
370 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP2, Types<int>);
371
372 // Tests that the same type-parameterized test case can be
373 // instantiated more than once in the same translation unit.
374 INSTANTIATE_TYPED_TEST_SUITE_P(Double, TypedTestP2, Types<double>);
375
376 // Tests that the same type-parameterized test case can be
377 // instantiated in different translation units linked together.
378 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
379 typedef Types<std::vector<double>, std::set<char> > MyContainers;
380 INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers);
381
382 // Tests that a type-parameterized test case can be defined and
383 // instantiated in a namespace.
384
385 namespace library2 {
386
387 template <typename T>
388 class NumericTest : public Test {};
389
390 TYPED_TEST_SUITE_P(NumericTest);
391
392 TYPED_TEST_P(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); }
393
394 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
395   EXPECT_LT(TypeParam(0), TypeParam(1));
396 }
397
398 REGISTER_TYPED_TEST_SUITE_P(NumericTest, DefaultIsZero, ZeroIsLessThanOne);
399 typedef Types<int, double> NumericTypes;
400 INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
401
402 static const char* GetTestName() {
403   return testing::UnitTest::GetInstance()->current_test_info()->name();
404 }
405 // Test the stripping of space from test names
406 template <typename T>
407 class TrimmedTest : public Test {};
408 TYPED_TEST_SUITE_P(TrimmedTest);
409 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
410 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
411 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
412 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
413 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
414 REGISTER_TYPED_TEST_SUITE_P(TrimmedTest, Test1, Test2, Test3, Test4,
415                             Test5);  // NOLINT
416 template <typename T1, typename T2>
417 struct MyPair {};
418 // Be sure to try a type with a comma in its name just in case it matters.
419 typedef Types<int, double, MyPair<int, int> > TrimTypes;
420 INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes);
421
422 }  // namespace library2