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