PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/Unittests/googletest/test/gtest-typed-test_test.cc

http://unladen-swallow.googlecode.com/
C++ | 360 lines | 175 code | 82 blank | 103 comment | 3 complexity | 9dbd84a837b777b7ca0852dcb1c42a69 MD5 | raw file
  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// Author: wan@google.com (Zhanyong Wan)
 31
 32#include <set>
 33#include <vector>
 34
 35#include "test/gtest-typed-test_test.h"
 36#include <gtest/gtest.h>
 37
 38using testing::Test;
 39
 40// Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
 41// ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
 42// type-parameterized test.
 43template <typename T>
 44class CommonTest : public Test {
 45  // For some technical reason, SetUpTestCase() and TearDownTestCase()
 46  // must be public.
 47 public:
 48  static void SetUpTestCase() {
 49    shared_ = new T(5);
 50  }
 51
 52  static void TearDownTestCase() {
 53    delete shared_;
 54    shared_ = NULL;
 55  }
 56
 57  // This 'protected:' is optional.  There's no harm in making all
 58  // members of this fixture class template public.
 59 protected:
 60  // We used to use std::list here, but switched to std::vector since
 61  // MSVC's <list> doesn't compile cleanly with /W4.
 62  typedef std::vector<T> Vector;
 63  typedef std::set<int> IntSet;
 64
 65  CommonTest() : value_(1) {}
 66
 67  virtual ~CommonTest() { EXPECT_EQ(3, value_); }
 68
 69  virtual void SetUp() {
 70    EXPECT_EQ(1, value_);
 71    value_++;
 72  }
 73
 74  virtual void TearDown() {
 75    EXPECT_EQ(2, value_);
 76    value_++;
 77  }
 78
 79  T value_;
 80  static T* shared_;
 81};
 82
 83template <typename T>
 84T* CommonTest<T>::shared_ = NULL;
 85
 86// This #ifdef block tests typed tests.
 87#if GTEST_HAS_TYPED_TEST
 88
 89using testing::Types;
 90
 91// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
 92// and SetUp()/TearDown() work correctly in typed tests
 93
 94typedef Types<char, int> TwoTypes;
 95TYPED_TEST_CASE(CommonTest, TwoTypes);
 96
 97TYPED_TEST(CommonTest, ValuesAreCorrect) {
 98  // Static members of the fixture class template can be visited via
 99  // the TestFixture:: prefix.
100  EXPECT_EQ(5, *TestFixture::shared_);
101
102  // Typedefs in the fixture class template can be visited via the
103  // "typename TestFixture::" prefix.
104  typename TestFixture::Vector empty;
105  EXPECT_EQ(0U, empty.size());
106
107  typename TestFixture::IntSet empty2;
108  EXPECT_EQ(0U, empty2.size());
109
110  // Non-static members of the fixture class must be visited via
111  // 'this', as required by C++ for class templates.
112  EXPECT_EQ(2, this->value_);
113}
114
115// The second test makes sure shared_ is not deleted after the first
116// test.
117TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
118  // Static members of the fixture class template can also be visited
119  // via 'this'.
120  ASSERT_TRUE(this->shared_ != NULL);
121  EXPECT_EQ(5, *this->shared_);
122
123  // TypeParam can be used to refer to the type parameter.
124  EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
125}
126
127// Tests that multiple TYPED_TEST_CASE's can be defined in the same
128// translation unit.
129
130template <typename T>
131class TypedTest1 : public Test {
132};
133
134// Verifies that the second argument of TYPED_TEST_CASE can be a
135// single type.
136TYPED_TEST_CASE(TypedTest1, int);
137TYPED_TEST(TypedTest1, A) {}
138
139template <typename T>
140class TypedTest2 : public Test {
141};
142
143// Verifies that the second argument of TYPED_TEST_CASE can be a
144// Types<...> type list.
145TYPED_TEST_CASE(TypedTest2, Types<int>);
146
147// This also verifies that tests from different typed test cases can
148// share the same name.
149TYPED_TEST(TypedTest2, A) {}
150
151// Tests that a typed test case can be defined in a namespace.
152
153namespace library1 {
154
155template <typename T>
156class NumericTest : public Test {
157};
158
159typedef Types<int, long> NumericTypes;
160TYPED_TEST_CASE(NumericTest, NumericTypes);
161
162TYPED_TEST(NumericTest, DefaultIsZero) {
163  EXPECT_EQ(0, TypeParam());
164}
165
166}  // namespace library1
167
168#endif  // GTEST_HAS_TYPED_TEST
169
170// This #ifdef block tests type-parameterized tests.
171#if GTEST_HAS_TYPED_TEST_P
172
173using testing::Types;
174using testing::internal::TypedTestCasePState;
175
176// Tests TypedTestCasePState.
177
178class TypedTestCasePStateTest : public Test {
179 protected:
180  virtual void SetUp() {
181    state_.AddTestName("foo.cc", 0, "FooTest", "A");
182    state_.AddTestName("foo.cc", 0, "FooTest", "B");
183    state_.AddTestName("foo.cc", 0, "FooTest", "C");
184  }
185
186  TypedTestCasePState state_;
187};
188
189TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
190  const char* tests = "A, B, C";
191  EXPECT_EQ(tests,
192            state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
193}
194
195// Makes sure that the order of the tests and spaces around the names
196// don't matter.
197TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
198  const char* tests = "A,C,   B";
199  EXPECT_EQ(tests,
200            state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
201}
202
203typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
204
205TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
206  EXPECT_DEATH_IF_SUPPORTED(
207      state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
208      "foo\\.cc.1.?: Test A is listed more than once\\.");
209}
210
211TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
212  EXPECT_DEATH_IF_SUPPORTED(
213      state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
214      "foo\\.cc.1.?: No test named D can be found in this test case\\.");
215}
216
217TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
218  EXPECT_DEATH_IF_SUPPORTED(
219      state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
220      "foo\\.cc.1.?: You forgot to list test B\\.");
221}
222
223// Tests that defining a test for a parameterized test case generates
224// a run-time error if the test case has been registered.
225TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
226  state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
227  EXPECT_DEATH_IF_SUPPORTED(
228      state_.AddTestName("foo.cc", 2, "FooTest", "D"),
229      "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
230      "\\(FooTest, \\.\\.\\.\\)\\.");
231}
232
233// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
234// and SetUp()/TearDown() work correctly in type-parameterized tests.
235
236template <typename T>
237class DerivedTest : public CommonTest<T> {
238};
239
240TYPED_TEST_CASE_P(DerivedTest);
241
242TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
243  // Static members of the fixture class template can be visited via
244  // the TestFixture:: prefix.
245  EXPECT_EQ(5, *TestFixture::shared_);
246
247  // Non-static members of the fixture class must be visited via
248  // 'this', as required by C++ for class templates.
249  EXPECT_EQ(2, this->value_);
250}
251
252// The second test makes sure shared_ is not deleted after the first
253// test.
254TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
255  // Static members of the fixture class template can also be visited
256  // via 'this'.
257  ASSERT_TRUE(this->shared_ != NULL);
258  EXPECT_EQ(5, *this->shared_);
259  EXPECT_EQ(2, this->value_);
260}
261
262REGISTER_TYPED_TEST_CASE_P(DerivedTest,
263                           ValuesAreCorrect, ValuesAreStillCorrect);
264
265typedef Types<short, long> MyTwoTypes;
266INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
267
268// Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
269// translation unit.
270
271template <typename T>
272class TypedTestP1 : public Test {
273};
274
275TYPED_TEST_CASE_P(TypedTestP1);
276
277// For testing that the code between TYPED_TEST_CASE_P() and
278// TYPED_TEST_P() is not enclosed in a namespace.
279typedef int IntAfterTypedTestCaseP;
280
281TYPED_TEST_P(TypedTestP1, A) {}
282TYPED_TEST_P(TypedTestP1, B) {}
283
284// For testing that the code between TYPED_TEST_P() and
285// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
286typedef int IntBeforeRegisterTypedTestCaseP;
287
288REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
289
290template <typename T>
291class TypedTestP2 : public Test {
292};
293
294TYPED_TEST_CASE_P(TypedTestP2);
295
296// This also verifies that tests from different type-parameterized
297// test cases can share the same name.
298TYPED_TEST_P(TypedTestP2, A) {}
299
300REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
301
302// Verifies that the code between TYPED_TEST_CASE_P() and
303// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
304IntAfterTypedTestCaseP after = 0;
305IntBeforeRegisterTypedTestCaseP before = 0;
306
307// Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
308// can be either a single type or a Types<...> type list.
309INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
310INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
311
312// Tests that the same type-parameterized test case can be
313// instantiated more than once in the same translation unit.
314INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
315
316// Tests that the same type-parameterized test case can be
317// instantiated in different translation units linked together.
318// (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
319typedef Types<std::vector<double>, std::set<char> > MyContainers;
320INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
321
322// Tests that a type-parameterized test case can be defined and
323// instantiated in a namespace.
324
325namespace library2 {
326
327template <typename T>
328class NumericTest : public Test {
329};
330
331TYPED_TEST_CASE_P(NumericTest);
332
333TYPED_TEST_P(NumericTest, DefaultIsZero) {
334  EXPECT_EQ(0, TypeParam());
335}
336
337TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
338  EXPECT_LT(TypeParam(0), TypeParam(1));
339}
340
341REGISTER_TYPED_TEST_CASE_P(NumericTest,
342                           DefaultIsZero, ZeroIsLessThanOne);
343typedef Types<int, double> NumericTypes;
344INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
345
346}  // namespace library2
347
348#endif  // GTEST_HAS_TYPED_TEST_P
349
350#if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
351
352// Google Test doesn't support type-parameterized tests on some platforms
353// and compilers, such as MSVC 7.1. If we use conditional compilation to
354// compile out all code referring to the gtest_main library, MSVC linker
355// will not link that library at all and consequently complain about
356// missing entry point defined in that library (fatal error LNK1561:
357// entry point must be defined). This dummy test keeps gtest_main linked in.
358TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
359
360#endif  // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)