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