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;
93 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
94 // and SetUp()/TearDown() work correctly in typed tests
96 typedef Types<char, int> TwoTypes;
97 TYPED_TEST_SUITE(CommonTest, TwoTypes);
99 TYPED_TEST(CommonTest, ValuesAreCorrect) {
100 // Static members of the fixture class template can be visited via
101 // the TestFixture:: prefix.
102 EXPECT_EQ(5, *TestFixture::shared_);
104 // Typedefs in the fixture class template can be visited via the
105 // "typename TestFixture::" prefix.
106 typename TestFixture::Vector empty;
107 EXPECT_EQ(0U, empty.size());
109 typename TestFixture::IntSet empty2;
110 EXPECT_EQ(0U, empty2.size());
112 // Non-static members of the fixture class must be visited via
113 // 'this', as required by C++ for class templates.
114 EXPECT_EQ(2, this->value_);
117 // The second test makes sure shared_ is not deleted after the first
119 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
120 // Static members of the fixture class template can also be visited
122 ASSERT_TRUE(this->shared_ != nullptr);
123 EXPECT_EQ(5, *this->shared_);
125 // TypeParam can be used to refer to the type parameter.
126 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
129 // Tests that multiple TYPED_TEST_SUITE's can be defined in the same
132 template <typename T>
133 class TypedTest1 : public Test {
136 // Verifies that the second argument of TYPED_TEST_SUITE can be a
138 TYPED_TEST_SUITE(TypedTest1, int);
139 TYPED_TEST(TypedTest1, A) {}
141 template <typename T>
142 class TypedTest2 : public Test {
145 // Verifies that the second argument of TYPED_TEST_SUITE can be a
146 // Types<...> type list.
147 TYPED_TEST_SUITE(TypedTest2, Types<int>);
149 // This also verifies that tests from different typed test cases can
150 // share the same name.
151 TYPED_TEST(TypedTest2, A) {}
153 // Tests that a typed test case can be defined in a namespace.
157 template <typename T>
158 class NumericTest : public Test {
161 typedef Types<int, long> NumericTypes;
162 TYPED_TEST_SUITE(NumericTest, NumericTypes);
164 TYPED_TEST(NumericTest, DefaultIsZero) {
165 EXPECT_EQ(0, TypeParam());
168 } // namespace library1
170 // Tests that custom names work.
171 template <typename T>
172 class TypedTestWithNames : public Test {};
174 class TypedTestNames {
176 template <typename T>
177 static std::string GetName(int i) {
178 if (std::is_same<T, char>::value) {
179 return std::string("char") + ::testing::PrintToString(i);
181 if (std::is_same<T, int>::value) {
182 return std::string("int") + ::testing::PrintToString(i);
187 TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
189 TYPED_TEST(TypedTestWithNames, TestSuiteName) {
190 if (std::is_same<TypeParam, char>::value) {
191 EXPECT_STREQ(::testing::UnitTest::GetInstance()
192 ->current_test_info()
194 "TypedTestWithNames/char0");
196 if (std::is_same<TypeParam, int>::value) {
197 EXPECT_STREQ(::testing::UnitTest::GetInstance()
198 ->current_test_info()
200 "TypedTestWithNames/int1");
204 using testing::Types;
205 using testing::internal::TypedTestSuitePState;
207 // Tests TypedTestSuitePState.
209 class TypedTestSuitePStateTest : public Test {
211 void SetUp() override {
212 state_.AddTestName("foo.cc", 0, "FooTest", "A");
213 state_.AddTestName("foo.cc", 0, "FooTest", "B");
214 state_.AddTestName("foo.cc", 0, "FooTest", "C");
217 TypedTestSuitePState state_;
220 TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
221 const char* tests = "A, B, C";
223 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
226 // Makes sure that the order of the tests and spaces around the names
228 TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
229 const char* tests = "A,C, B";
231 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
234 using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
236 TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
237 EXPECT_DEATH_IF_SUPPORTED(
238 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
239 "foo\\.cc.1.?: Test A is listed more than once\\.");
242 TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
243 EXPECT_DEATH_IF_SUPPORTED(
244 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C, D"),
245 "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
248 TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
249 EXPECT_DEATH_IF_SUPPORTED(
250 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, C"),
251 "foo\\.cc.1.?: You forgot to list test B\\.");
254 // Tests that defining a test for a parameterized test case generates
255 // a run-time error if the test case has been registered.
256 TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
257 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
258 EXPECT_DEATH_IF_SUPPORTED(
259 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
260 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
261 "\\(FooTest, \\.\\.\\.\\)\\.");
264 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
265 // and SetUp()/TearDown() work correctly in type-parameterized tests.
267 template <typename T>
268 class DerivedTest : public CommonTest<T> {
271 TYPED_TEST_SUITE_P(DerivedTest);
273 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
274 // Static members of the fixture class template can be visited via
275 // the TestFixture:: prefix.
276 EXPECT_EQ(5, *TestFixture::shared_);
278 // Non-static members of the fixture class must be visited via
279 // 'this', as required by C++ for class templates.
280 EXPECT_EQ(2, this->value_);
283 // The second test makes sure shared_ is not deleted after the first
285 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
286 // Static members of the fixture class template can also be visited
288 ASSERT_TRUE(this->shared_ != nullptr);
289 EXPECT_EQ(5, *this->shared_);
290 EXPECT_EQ(2, this->value_);
293 REGISTER_TYPED_TEST_SUITE_P(DerivedTest,
294 ValuesAreCorrect, ValuesAreStillCorrect);
296 typedef Types<short, long> MyTwoTypes;
297 INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
299 // Tests that custom names work with type parametrized tests. We reuse the
300 // TwoTypes from above here.
301 template <typename T>
302 class TypeParametrizedTestWithNames : public Test {};
304 TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
306 TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
307 if (std::is_same<TypeParam, char>::value) {
308 EXPECT_STREQ(::testing::UnitTest::GetInstance()
309 ->current_test_info()
311 "CustomName/TypeParametrizedTestWithNames/parChar0");
313 if (std::is_same<TypeParam, int>::value) {
314 EXPECT_STREQ(::testing::UnitTest::GetInstance()
315 ->current_test_info()
317 "CustomName/TypeParametrizedTestWithNames/parInt1");
321 REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
323 class TypeParametrizedTestNames {
325 template <typename T>
326 static std::string GetName(int i) {
327 if (std::is_same<T, char>::value) {
328 return std::string("parChar") + ::testing::PrintToString(i);
330 if (std::is_same<T, int>::value) {
331 return std::string("parInt") + ::testing::PrintToString(i);
336 INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames,
337 TwoTypes, TypeParametrizedTestNames);
339 // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
342 template <typename T>
343 class TypedTestP1 : public Test {
346 TYPED_TEST_SUITE_P(TypedTestP1);
348 // For testing that the code between TYPED_TEST_SUITE_P() and
349 // TYPED_TEST_P() is not enclosed in a namespace.
350 using IntAfterTypedTestSuiteP = int;
352 TYPED_TEST_P(TypedTestP1, A) {}
353 TYPED_TEST_P(TypedTestP1, B) {}
355 // For testing that the code between TYPED_TEST_P() and
356 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
357 using IntBeforeRegisterTypedTestSuiteP = int;
359 REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
361 template <typename T>
362 class TypedTestP2 : public Test {
365 TYPED_TEST_SUITE_P(TypedTestP2);
367 // This also verifies that tests from different type-parameterized
368 // test cases can share the same name.
369 TYPED_TEST_P(TypedTestP2, A) {}
371 REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
373 // Verifies that the code between TYPED_TEST_SUITE_P() and
374 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
375 IntAfterTypedTestSuiteP after = 0;
376 IntBeforeRegisterTypedTestSuiteP before = 0;
378 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
379 // can be either a single type or a Types<...> type list.
380 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
381 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP2, Types<int>);
383 // Tests that the same type-parameterized test case can be
384 // instantiated more than once in the same translation unit.
385 INSTANTIATE_TYPED_TEST_SUITE_P(Double, TypedTestP2, Types<double>);
387 // Tests that the same type-parameterized test case can be
388 // instantiated in different translation units linked together.
389 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
390 typedef Types<std::vector<double>, std::set<char> > MyContainers;
391 INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers);
393 // Tests that a type-parameterized test case can be defined and
394 // instantiated in a namespace.
398 template <typename T>
399 class NumericTest : public Test {
402 TYPED_TEST_SUITE_P(NumericTest);
404 TYPED_TEST_P(NumericTest, DefaultIsZero) {
405 EXPECT_EQ(0, TypeParam());
408 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
409 EXPECT_LT(TypeParam(0), TypeParam(1));
412 REGISTER_TYPED_TEST_SUITE_P(NumericTest,
413 DefaultIsZero, ZeroIsLessThanOne);
414 typedef Types<int, double> NumericTypes;
415 INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
417 static const char* GetTestName() {
418 return testing::UnitTest::GetInstance()->current_test_info()->name();
420 // Test the stripping of space from test names
421 template <typename T> class TrimmedTest : public Test { };
422 TYPED_TEST_SUITE_P(TrimmedTest);
423 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
424 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
425 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
426 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
427 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
428 REGISTER_TYPED_TEST_SUITE_P(
430 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
431 template <typename T1, typename T2> struct MyPair {};
432 // Be sure to try a type with a comma in its name just in case it matters.
433 typedef Types<int, double, MyPair<int, int> > TrimTypes;
434 INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes);
436 } // namespace library2