[12746] | 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 |
|
---|
| 38 | using 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.
|
---|
| 43 | template <typename T>
|
---|
| 44 | class 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 |
|
---|
| 83 | template <typename T>
|
---|
| 84 | T* CommonTest<T>::shared_ = NULL;
|
---|
| 85 |
|
---|
| 86 | // This #ifdef block tests typed tests.
|
---|
| 87 | #if GTEST_HAS_TYPED_TEST
|
---|
| 88 |
|
---|
| 89 | using testing::Types;
|
---|
| 90 |
|
---|
| 91 | // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
|
---|
| 92 | // and SetUp()/TearDown() work correctly in typed tests
|
---|
| 93 |
|
---|
| 94 | typedef Types<char, int> TwoTypes;
|
---|
| 95 | TYPED_TEST_CASE(CommonTest, TwoTypes);
|
---|
| 96 |
|
---|
| 97 | TYPED_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.
|
---|
| 117 | TYPED_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 |
|
---|
| 130 | template <typename T>
|
---|
| 131 | class TypedTest1 : public Test {
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 | // Verifies that the second argument of TYPED_TEST_CASE can be a
|
---|
| 135 | // single type.
|
---|
| 136 | TYPED_TEST_CASE(TypedTest1, int);
|
---|
| 137 | TYPED_TEST(TypedTest1, A) {}
|
---|
| 138 |
|
---|
| 139 | template <typename T>
|
---|
| 140 | class TypedTest2 : public Test {
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 | // Verifies that the second argument of TYPED_TEST_CASE can be a
|
---|
| 144 | // Types<...> type list.
|
---|
| 145 | TYPED_TEST_CASE(TypedTest2, Types<int>);
|
---|
| 146 |
|
---|
| 147 | // This also verifies that tests from different typed test cases can
|
---|
| 148 | // share the same name.
|
---|
| 149 | TYPED_TEST(TypedTest2, A) {}
|
---|
| 150 |
|
---|
| 151 | // Tests that a typed test case can be defined in a namespace.
|
---|
| 152 |
|
---|
| 153 | namespace library1 {
|
---|
| 154 |
|
---|
| 155 | template <typename T>
|
---|
| 156 | class NumericTest : public Test {
|
---|
| 157 | };
|
---|
| 158 |
|
---|
| 159 | typedef Types<int, long> NumericTypes;
|
---|
| 160 | TYPED_TEST_CASE(NumericTest, NumericTypes);
|
---|
| 161 |
|
---|
| 162 | TYPED_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 |
|
---|
| 173 | using testing::Types;
|
---|
| 174 | using testing::internal::TypedTestCasePState;
|
---|
| 175 |
|
---|
| 176 | // Tests TypedTestCasePState.
|
---|
| 177 |
|
---|
| 178 | class 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 |
|
---|
| 189 | TEST_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.
|
---|
| 197 | TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
|
---|
| 198 | const char* tests = "A,C, B";
|
---|
| 199 | EXPECT_EQ(tests,
|
---|
| 200 | state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
|
---|
| 204 |
|
---|
| 205 | TEST_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 |
|
---|
| 211 | TEST_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 |
|
---|
| 217 | TEST_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.
|
---|
| 225 | TEST_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 |
|
---|
| 236 | template <typename T>
|
---|
| 237 | class DerivedTest : public CommonTest<T> {
|
---|
| 238 | };
|
---|
| 239 |
|
---|
| 240 | TYPED_TEST_CASE_P(DerivedTest);
|
---|
| 241 |
|
---|
| 242 | TYPED_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.
|
---|
| 254 | TYPED_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 |
|
---|
| 262 | REGISTER_TYPED_TEST_CASE_P(DerivedTest,
|
---|
| 263 | ValuesAreCorrect, ValuesAreStillCorrect);
|
---|
| 264 |
|
---|
| 265 | typedef Types<short, long> MyTwoTypes;
|
---|
| 266 | INSTANTIATE_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 |
|
---|
| 271 | template <typename T>
|
---|
| 272 | class TypedTestP1 : public Test {
|
---|
| 273 | };
|
---|
| 274 |
|
---|
| 275 | TYPED_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.
|
---|
| 279 | typedef int IntAfterTypedTestCaseP;
|
---|
| 280 |
|
---|
| 281 | TYPED_TEST_P(TypedTestP1, A) {}
|
---|
| 282 | TYPED_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.
|
---|
| 286 | typedef int IntBeforeRegisterTypedTestCaseP;
|
---|
| 287 |
|
---|
| 288 | REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
|
---|
| 289 |
|
---|
| 290 | template <typename T>
|
---|
| 291 | class TypedTestP2 : public Test {
|
---|
| 292 | };
|
---|
| 293 |
|
---|
| 294 | TYPED_TEST_CASE_P(TypedTestP2);
|
---|
| 295 |
|
---|
| 296 | // This also verifies that tests from different type-parameterized
|
---|
| 297 | // test cases can share the same name.
|
---|
| 298 | TYPED_TEST_P(TypedTestP2, A) {}
|
---|
| 299 |
|
---|
| 300 | REGISTER_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.
|
---|
| 304 | IntAfterTypedTestCaseP after = 0;
|
---|
| 305 | IntBeforeRegisterTypedTestCaseP 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.
|
---|
| 309 | INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
|
---|
| 310 | INSTANTIATE_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.
|
---|
| 314 | INSTANTIATE_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.)
|
---|
| 319 | typedef Types<std::vector<double>, std::set<char> > MyContainers;
|
---|
| 320 | INSTANTIATE_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 |
|
---|
| 325 | namespace library2 {
|
---|
| 326 |
|
---|
| 327 | template <typename T>
|
---|
| 328 | class NumericTest : public Test {
|
---|
| 329 | };
|
---|
| 330 |
|
---|
| 331 | TYPED_TEST_CASE_P(NumericTest);
|
---|
| 332 |
|
---|
| 333 | TYPED_TEST_P(NumericTest, DefaultIsZero) {
|
---|
| 334 | EXPECT_EQ(0, TypeParam());
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
|
---|
| 338 | EXPECT_LT(TypeParam(0), TypeParam(1));
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | REGISTER_TYPED_TEST_CASE_P(NumericTest,
|
---|
| 342 | DefaultIsZero, ZeroIsLessThanOne);
|
---|
| 343 | typedef Types<int, double> NumericTypes;
|
---|
| 344 | INSTANTIATE_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 may not support type-parameterized tests with some
|
---|
| 353 | // compilers. If we use conditional compilation to compile out all
|
---|
| 354 | // code referring to the gtest_main library, MSVC linker will not link
|
---|
| 355 | // that library at all and consequently complain about missing entry
|
---|
| 356 | // point defined in that library (fatal error LNK1561: entry point
|
---|
| 357 | // must be defined). This dummy test keeps gtest_main linked in.
|
---|
| 358 | TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
|
---|
| 359 |
|
---|
| 360 | #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
|
---|