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.
30 #include "test/gtest-typed-test_test.h"
33 #include <type_traits>
36 #include "gtest/gtest.h"
39 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
44 // Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture
45 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
46 // type-parameterized test.
48 class CommonTest : public Test {
49 // For some technical reason, SetUpTestSuite() and TearDownTestSuite()
52 static void SetUpTestSuite() { shared_ = new T(5); }
54 static void TearDownTestSuite() {
59 // This 'protected:' is optional. There's no harm in making all
60 // members of this fixture class template public.
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;
67 CommonTest() : value_(1) {}
69 ~CommonTest() override { EXPECT_EQ(3, value_); }
71 void SetUp() override {
76 void TearDown() override {
86 T* CommonTest<T>::shared_ = nullptr;
90 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
91 // and SetUp()/TearDown() work correctly in typed tests
93 typedef Types<char, int> TwoTypes;
94 TYPED_TEST_SUITE(CommonTest, TwoTypes);
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_);
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());
106 typename TestFixture::IntSet empty2;
107 EXPECT_EQ(0U, empty2.size());
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_);
114 // The second test makes sure shared_ is not deleted after the first
116 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
117 // Static members of the fixture class template can also be visited
119 ASSERT_TRUE(this->shared_ != nullptr);
120 EXPECT_EQ(5, *this->shared_);
122 // TypeParam can be used to refer to the type parameter.
123 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
126 // Tests that multiple TYPED_TEST_SUITE's can be defined in the same
129 template <typename T>
130 class TypedTest1 : public Test {};
132 // Verifies that the second argument of TYPED_TEST_SUITE can be a
134 TYPED_TEST_SUITE(TypedTest1, int);
135 TYPED_TEST(TypedTest1, A) {}
137 template <typename T>
138 class TypedTest2 : public Test {};
140 // Verifies that the second argument of TYPED_TEST_SUITE can be a
141 // Types<...> type list.
142 TYPED_TEST_SUITE(TypedTest2, Types<int>);
144 // This also verifies that tests from different typed test cases can
145 // share the same name.
146 TYPED_TEST(TypedTest2, A) {}
148 // Tests that a typed test case can be defined in a namespace.
152 template <typename T>
153 class NumericTest : public Test {};
155 typedef Types<int, long> NumericTypes;
156 TYPED_TEST_SUITE(NumericTest, NumericTypes);
158 TYPED_TEST(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); }
160 } // namespace library1
162 // Tests that custom names work.
163 template <typename T>
164 class TypedTestWithNames : public Test {};
166 class TypedTestNames {
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);
173 if (std::is_same<T, int>::value) {
174 return std::string("int") + ::testing::PrintToString(i);
179 TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
181 TYPED_TEST(TypedTestWithNames, TestSuiteName) {
182 if (std::is_same<TypeParam, char>::value) {
183 EXPECT_STREQ(::testing::UnitTest::GetInstance()
184 ->current_test_info()
186 "TypedTestWithNames/char0");
188 if (std::is_same<TypeParam, int>::value) {
189 EXPECT_STREQ(::testing::UnitTest::GetInstance()
190 ->current_test_info()
192 "TypedTestWithNames/int1");
196 using testing::Types;
197 using testing::internal::TypedTestSuitePState;
199 // Tests TypedTestSuitePState.
201 class TypedTestSuitePStateTest : public Test {
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");
209 TypedTestSuitePState state_;
212 TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
213 const char* tests = "A, B, C";
215 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
218 // Makes sure that the order of the tests and spaces around the names
220 TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
221 const char* tests = "A,C, B";
223 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
226 using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
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\\.");
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\\.");
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\\.");
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, \\.\\.\\.\\)\\.");
256 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
257 // and SetUp()/TearDown() work correctly in type-parameterized tests.
259 template <typename T>
260 class DerivedTest : public CommonTest<T> {};
262 TYPED_TEST_SUITE_P(DerivedTest);
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_);
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_);
274 // The second test makes sure shared_ is not deleted after the first
276 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
277 // Static members of the fixture class template can also be visited
279 ASSERT_TRUE(this->shared_ != nullptr);
280 EXPECT_EQ(5, *this->shared_);
281 EXPECT_EQ(2, this->value_);
284 REGISTER_TYPED_TEST_SUITE_P(DerivedTest, ValuesAreCorrect,
285 ValuesAreStillCorrect);
287 typedef Types<short, long> MyTwoTypes;
288 INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
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 {};
295 TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
297 TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
298 if (std::is_same<TypeParam, char>::value) {
299 EXPECT_STREQ(::testing::UnitTest::GetInstance()
300 ->current_test_info()
302 "CustomName/TypeParametrizedTestWithNames/parChar0");
304 if (std::is_same<TypeParam, int>::value) {
305 EXPECT_STREQ(::testing::UnitTest::GetInstance()
306 ->current_test_info()
308 "CustomName/TypeParametrizedTestWithNames/parInt1");
312 REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
314 class TypeParametrizedTestNames {
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);
321 if (std::is_same<T, int>::value) {
322 return std::string("parInt") + ::testing::PrintToString(i);
327 INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames,
328 TwoTypes, TypeParametrizedTestNames);
330 // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
333 template <typename T>
334 class TypedTestP1 : public Test {};
336 TYPED_TEST_SUITE_P(TypedTestP1);
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;
342 TYPED_TEST_P(TypedTestP1, A) {}
343 TYPED_TEST_P(TypedTestP1, B) {}
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;
349 REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
351 template <typename T>
352 class TypedTestP2 : public Test {};
354 TYPED_TEST_SUITE_P(TypedTestP2);
356 // This also verifies that tests from different type-parameterized
357 // test cases can share the same name.
358 TYPED_TEST_P(TypedTestP2, A) {}
360 REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
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;
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>);
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>);
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);
382 // Tests that a type-parameterized test case can be defined and
383 // instantiated in a namespace.
387 template <typename T>
388 class NumericTest : public Test {};
390 TYPED_TEST_SUITE_P(NumericTest);
392 TYPED_TEST_P(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); }
394 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
395 EXPECT_LT(TypeParam(0), TypeParam(1));
398 REGISTER_TYPED_TEST_SUITE_P(NumericTest, DefaultIsZero, ZeroIsLessThanOne);
399 typedef Types<int, double> NumericTypes;
400 INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
402 static const char* GetTestName() {
403 return testing::UnitTest::GetInstance()->current_test_info()->name();
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,
416 template <typename T1, typename T2>
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);
422 } // namespace library2