1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #include "test/gtest-typed-test_test.h"
34 #include <type_traits>
37 #include "gtest/gtest.h"
40 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
45 // Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture
46 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
47 // type-parameterized test.
49 class CommonTest : public Test {
50 // For some technical reason, SetUpTestSuite() and TearDownTestSuite()
53 static void SetUpTestSuite() {
57 static void TearDownTestSuite() {
62 // This 'protected:' is optional. There's no harm in making all
63 // members of this fixture class template public.
65 // We used to use std::list here, but switched to std::vector since
66 // MSVC's <list> doesn't compile cleanly with /W4.
67 typedef std::vector<T> Vector;
68 typedef std::set<int> IntSet;
70 CommonTest() : value_(1) {}
72 ~CommonTest() override { EXPECT_EQ(3, value_); }
74 void SetUp() override {
79 void TearDown() override {
89 T* CommonTest<T>::shared_ = nullptr;
91 // This #ifdef block tests typed tests.
92 #if GTEST_HAS_TYPED_TEST
96 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
97 // and SetUp()/TearDown() work correctly in typed tests
99 typedef Types<char, int> TwoTypes;
100 TYPED_TEST_SUITE(CommonTest, TwoTypes);
102 TYPED_TEST(CommonTest, ValuesAreCorrect) {
103 // Static members of the fixture class template can be visited via
104 // the TestFixture:: prefix.
105 EXPECT_EQ(5, *TestFixture::shared_);
107 // Typedefs in the fixture class template can be visited via the
108 // "typename TestFixture::" prefix.
109 typename TestFixture::Vector empty;
110 EXPECT_EQ(0U, empty.size());
112 typename TestFixture::IntSet empty2;
113 EXPECT_EQ(0U, empty2.size());
115 // Non-static members of the fixture class must be visited via
116 // 'this', as required by C++ for class templates.
117 EXPECT_EQ(2, this->value_);
120 // The second test makes sure shared_ is not deleted after the first
122 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
123 // Static members of the fixture class template can also be visited
125 ASSERT_TRUE(this->shared_ != nullptr);
126 EXPECT_EQ(5, *this->shared_);
128 // TypeParam can be used to refer to the type parameter.
129 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
132 // Tests that multiple TYPED_TEST_SUITE's can be defined in the same
135 template <typename T>
136 class TypedTest1 : public Test {
139 // Verifies that the second argument of TYPED_TEST_SUITE can be a
141 TYPED_TEST_SUITE(TypedTest1, int);
142 TYPED_TEST(TypedTest1, A) {}
144 template <typename T>
145 class TypedTest2 : public Test {
148 // Verifies that the second argument of TYPED_TEST_SUITE can be a
149 // Types<...> type list.
150 TYPED_TEST_SUITE(TypedTest2, Types<int>);
152 // This also verifies that tests from different typed test cases can
153 // share the same name.
154 TYPED_TEST(TypedTest2, A) {}
156 // Tests that a typed test case can be defined in a namespace.
160 template <typename T>
161 class NumericTest : public Test {
164 typedef Types<int, long> NumericTypes;
165 TYPED_TEST_SUITE(NumericTest, NumericTypes);
167 TYPED_TEST(NumericTest, DefaultIsZero) {
168 EXPECT_EQ(0, TypeParam());
171 } // namespace library1
173 // Tests that custom names work.
174 template <typename T>
175 class TypedTestWithNames : public Test {};
177 class TypedTestNames {
179 template <typename T>
180 static std::string GetName(int i) {
181 if (std::is_same<T, char>::value) {
182 return std::string("char") + ::testing::PrintToString(i);
184 if (std::is_same<T, int>::value) {
185 return std::string("int") + ::testing::PrintToString(i);
190 TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
192 TYPED_TEST(TypedTestWithNames, TestSuiteName) {
193 if (std::is_same<TypeParam, char>::value) {
194 EXPECT_STREQ(::testing::UnitTest::GetInstance()
195 ->current_test_info()
197 "TypedTestWithNames/char0");
199 if (std::is_same<TypeParam, int>::value) {
200 EXPECT_STREQ(::testing::UnitTest::GetInstance()
201 ->current_test_info()
203 "TypedTestWithNames/int1");
207 #endif // GTEST_HAS_TYPED_TEST
209 // This #ifdef block tests type-parameterized tests.
210 #if GTEST_HAS_TYPED_TEST_P
212 using testing::Types;
213 using testing::internal::TypedTestSuitePState;
215 // Tests TypedTestSuitePState.
217 class TypedTestSuitePStateTest : public Test {
219 void SetUp() override {
220 state_.AddTestName("foo.cc", 0, "FooTest", "A");
221 state_.AddTestName("foo.cc", 0, "FooTest", "B");
222 state_.AddTestName("foo.cc", 0, "FooTest", "C");
225 TypedTestSuitePState state_;
228 TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
229 const char* tests = "A, B, C";
231 state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
234 // Makes sure that the order of the tests and spaces around the names
236 TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
237 const char* tests = "A,C, B";
239 state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
242 using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
244 TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
245 EXPECT_DEATH_IF_SUPPORTED(
246 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
247 "foo\\.cc.1.?: Test A is listed more than once\\.");
250 TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
251 EXPECT_DEATH_IF_SUPPORTED(
252 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
253 "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
256 TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
257 EXPECT_DEATH_IF_SUPPORTED(
258 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
259 "foo\\.cc.1.?: You forgot to list test B\\.");
262 // Tests that defining a test for a parameterized test case generates
263 // a run-time error if the test case has been registered.
264 TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
265 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
266 EXPECT_DEATH_IF_SUPPORTED(
267 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
268 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
269 "\\(FooTest, \\.\\.\\.\\)\\.");
272 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
273 // and SetUp()/TearDown() work correctly in type-parameterized tests.
275 template <typename T>
276 class DerivedTest : public CommonTest<T> {
279 TYPED_TEST_SUITE_P(DerivedTest);
281 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
282 // Static members of the fixture class template can be visited via
283 // the TestFixture:: prefix.
284 EXPECT_EQ(5, *TestFixture::shared_);
286 // Non-static members of the fixture class must be visited via
287 // 'this', as required by C++ for class templates.
288 EXPECT_EQ(2, this->value_);
291 // The second test makes sure shared_ is not deleted after the first
293 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
294 // Static members of the fixture class template can also be visited
296 ASSERT_TRUE(this->shared_ != nullptr);
297 EXPECT_EQ(5, *this->shared_);
298 EXPECT_EQ(2, this->value_);
301 REGISTER_TYPED_TEST_SUITE_P(DerivedTest,
302 ValuesAreCorrect, ValuesAreStillCorrect);
304 typedef Types<short, long> MyTwoTypes;
305 INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
307 // Tests that custom names work with type parametrized tests. We reuse the
308 // TwoTypes from above here.
309 template <typename T>
310 class TypeParametrizedTestWithNames : public Test {};
312 TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
314 TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
315 if (std::is_same<TypeParam, char>::value) {
316 EXPECT_STREQ(::testing::UnitTest::GetInstance()
317 ->current_test_info()
319 "CustomName/TypeParametrizedTestWithNames/parChar0");
321 if (std::is_same<TypeParam, int>::value) {
322 EXPECT_STREQ(::testing::UnitTest::GetInstance()
323 ->current_test_info()
325 "CustomName/TypeParametrizedTestWithNames/parInt1");
329 REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
331 class TypeParametrizedTestNames {
333 template <typename T>
334 static std::string GetName(int i) {
335 if (std::is_same<T, char>::value) {
336 return std::string("parChar") + ::testing::PrintToString(i);
338 if (std::is_same<T, int>::value) {
339 return std::string("parInt") + ::testing::PrintToString(i);
344 INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames,
345 TwoTypes, TypeParametrizedTestNames);
347 // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
350 template <typename T>
351 class TypedTestP1 : public Test {
354 TYPED_TEST_SUITE_P(TypedTestP1);
356 // For testing that the code between TYPED_TEST_SUITE_P() and
357 // TYPED_TEST_P() is not enclosed in a namespace.
358 using IntAfterTypedTestSuiteP = int;
360 TYPED_TEST_P(TypedTestP1, A) {}
361 TYPED_TEST_P(TypedTestP1, B) {}
363 // For testing that the code between TYPED_TEST_P() and
364 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
365 using IntBeforeRegisterTypedTestSuiteP = int;
367 REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
369 template <typename T>
370 class TypedTestP2 : public Test {
373 TYPED_TEST_SUITE_P(TypedTestP2);
375 // This also verifies that tests from different type-parameterized
376 // test cases can share the same name.
377 TYPED_TEST_P(TypedTestP2, A) {}
379 REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
381 // Verifies that the code between TYPED_TEST_SUITE_P() and
382 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
383 IntAfterTypedTestSuiteP after = 0;
384 IntBeforeRegisterTypedTestSuiteP before = 0;
386 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
387 // can be either a single type or a Types<...> type list.
388 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
389 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP2, Types<int>);
391 // Tests that the same type-parameterized test case can be
392 // instantiated more than once in the same translation unit.
393 INSTANTIATE_TYPED_TEST_SUITE_P(Double, TypedTestP2, Types<double>);
395 // Tests that the same type-parameterized test case can be
396 // instantiated in different translation units linked together.
397 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
398 typedef Types<std::vector<double>, std::set<char> > MyContainers;
399 INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers);
401 // Tests that a type-parameterized test case can be defined and
402 // instantiated in a namespace.
406 template <typename T>
407 class NumericTest : public Test {
410 TYPED_TEST_SUITE_P(NumericTest);
412 TYPED_TEST_P(NumericTest, DefaultIsZero) {
413 EXPECT_EQ(0, TypeParam());
416 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
417 EXPECT_LT(TypeParam(0), TypeParam(1));
420 REGISTER_TYPED_TEST_SUITE_P(NumericTest,
421 DefaultIsZero, ZeroIsLessThanOne);
422 typedef Types<int, double> NumericTypes;
423 INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
425 static const char* GetTestName() {
426 return testing::UnitTest::GetInstance()->current_test_info()->name();
428 // Test the stripping of space from test names
429 template <typename T> class TrimmedTest : public Test { };
430 TYPED_TEST_SUITE_P(TrimmedTest);
431 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
432 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
433 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
434 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
435 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
436 REGISTER_TYPED_TEST_SUITE_P(
438 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
439 template <typename T1, typename T2> struct MyPair {};
440 // Be sure to try a type with a comma in its name just in case it matters.
441 typedef Types<int, double, MyPair<int, int> > TrimTypes;
442 INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes);
444 } // namespace library2
446 #endif // GTEST_HAS_TYPED_TEST_P
448 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
450 // Google Test may not support type-parameterized tests with some
451 // compilers. If we use conditional compilation to compile out all
452 // code referring to the gtest_main library, MSVC linker will not link
453 // that library at all and consequently complain about missing entry
454 // point defined in that library (fatal error LNK1561: entry point
455 // must be defined). This dummy test keeps gtest_main linked in.
456 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
459 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
462 #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)