michael@0: // Copyright 2008 Google Inc. michael@0: // All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: // This sample shows how to test common properties of multiple michael@0: // implementations of the same interface (aka interface tests). michael@0: michael@0: // The interface and its implementations are in this header. michael@0: #include "prime_tables.h" michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: // First, we define some factory functions for creating instances of michael@0: // the implementations. You may be able to skip this step if all your michael@0: // implementations can be constructed the same way. michael@0: michael@0: template michael@0: PrimeTable* CreatePrimeTable(); michael@0: michael@0: template <> michael@0: PrimeTable* CreatePrimeTable() { michael@0: return new OnTheFlyPrimeTable; michael@0: } michael@0: michael@0: template <> michael@0: PrimeTable* CreatePrimeTable() { michael@0: return new PreCalculatedPrimeTable(10000); michael@0: } michael@0: michael@0: // Then we define a test fixture class template. michael@0: template michael@0: class PrimeTableTest : public testing::Test { michael@0: protected: michael@0: // The ctor calls the factory function to create a prime table michael@0: // implemented by T. michael@0: PrimeTableTest() : table_(CreatePrimeTable()) {} michael@0: michael@0: virtual ~PrimeTableTest() { delete table_; } michael@0: michael@0: // Note that we test an implementation via the base interface michael@0: // instead of the actual implementation class. This is important michael@0: // for keeping the tests close to the real world scenario, where the michael@0: // implementation is invoked via the base interface. It avoids michael@0: // got-yas where the implementation class has a method that shadows michael@0: // a method with the same name (but slightly different argument michael@0: // types) in the base interface, for example. michael@0: PrimeTable* const table_; michael@0: }; michael@0: michael@0: #if GTEST_HAS_TYPED_TEST michael@0: michael@0: using testing::Types; michael@0: michael@0: // Google Test offers two ways for reusing tests for different types. michael@0: // The first is called "typed tests". You should use it if you michael@0: // already know *all* the types you are gonna exercise when you write michael@0: // the tests. michael@0: michael@0: // To write a typed test case, first use michael@0: // michael@0: // TYPED_TEST_CASE(TestCaseName, TypeList); michael@0: // michael@0: // to declare it and specify the type parameters. As with TEST_F, michael@0: // TestCaseName must match the test fixture name. michael@0: michael@0: // The list of types we want to test. michael@0: typedef Types Implementations; michael@0: michael@0: TYPED_TEST_CASE(PrimeTableTest, Implementations); michael@0: michael@0: // Then use TYPED_TEST(TestCaseName, TestName) to define a typed test, michael@0: // similar to TEST_F. michael@0: TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) { michael@0: // Inside the test body, you can refer to the type parameter by michael@0: // TypeParam, and refer to the fixture class by TestFixture. We michael@0: // don't need them in this example. michael@0: michael@0: // Since we are in the template world, C++ requires explicitly michael@0: // writing 'this->' when referring to members of the fixture class. michael@0: // This is something you have to learn to live with. michael@0: EXPECT_FALSE(this->table_->IsPrime(-5)); michael@0: EXPECT_FALSE(this->table_->IsPrime(0)); michael@0: EXPECT_FALSE(this->table_->IsPrime(1)); michael@0: EXPECT_FALSE(this->table_->IsPrime(4)); michael@0: EXPECT_FALSE(this->table_->IsPrime(6)); michael@0: EXPECT_FALSE(this->table_->IsPrime(100)); michael@0: } michael@0: michael@0: TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) { michael@0: EXPECT_TRUE(this->table_->IsPrime(2)); michael@0: EXPECT_TRUE(this->table_->IsPrime(3)); michael@0: EXPECT_TRUE(this->table_->IsPrime(5)); michael@0: EXPECT_TRUE(this->table_->IsPrime(7)); michael@0: EXPECT_TRUE(this->table_->IsPrime(11)); michael@0: EXPECT_TRUE(this->table_->IsPrime(131)); michael@0: } michael@0: michael@0: TYPED_TEST(PrimeTableTest, CanGetNextPrime) { michael@0: EXPECT_EQ(2, this->table_->GetNextPrime(0)); michael@0: EXPECT_EQ(3, this->table_->GetNextPrime(2)); michael@0: EXPECT_EQ(5, this->table_->GetNextPrime(3)); michael@0: EXPECT_EQ(7, this->table_->GetNextPrime(5)); michael@0: EXPECT_EQ(11, this->table_->GetNextPrime(7)); michael@0: EXPECT_EQ(131, this->table_->GetNextPrime(128)); michael@0: } michael@0: michael@0: // That's it! Google Test will repeat each TYPED_TEST for each type michael@0: // in the type list specified in TYPED_TEST_CASE. Sit back and be michael@0: // happy that you don't have to define them multiple times. michael@0: michael@0: #endif // GTEST_HAS_TYPED_TEST michael@0: michael@0: #if GTEST_HAS_TYPED_TEST_P michael@0: michael@0: using testing::Types; michael@0: michael@0: // Sometimes, however, you don't yet know all the types that you want michael@0: // to test when you write the tests. For example, if you are the michael@0: // author of an interface and expect other people to implement it, you michael@0: // might want to write a set of tests to make sure each implementation michael@0: // conforms to some basic requirements, but you don't know what michael@0: // implementations will be written in the future. michael@0: // michael@0: // How can you write the tests without committing to the type michael@0: // parameters? That's what "type-parameterized tests" can do for you. michael@0: // It is a bit more involved than typed tests, but in return you get a michael@0: // test pattern that can be reused in many contexts, which is a big michael@0: // win. Here's how you do it: michael@0: michael@0: // First, define a test fixture class template. Here we just reuse michael@0: // the PrimeTableTest fixture defined earlier: michael@0: michael@0: template michael@0: class PrimeTableTest2 : public PrimeTableTest { michael@0: }; michael@0: michael@0: // Then, declare the test case. The argument is the name of the test michael@0: // fixture, and also the name of the test case (as usual). The _P michael@0: // suffix is for "parameterized" or "pattern". michael@0: TYPED_TEST_CASE_P(PrimeTableTest2); michael@0: michael@0: // Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test, michael@0: // similar to what you do with TEST_F. michael@0: TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) { michael@0: EXPECT_FALSE(this->table_->IsPrime(-5)); michael@0: EXPECT_FALSE(this->table_->IsPrime(0)); michael@0: EXPECT_FALSE(this->table_->IsPrime(1)); michael@0: EXPECT_FALSE(this->table_->IsPrime(4)); michael@0: EXPECT_FALSE(this->table_->IsPrime(6)); michael@0: EXPECT_FALSE(this->table_->IsPrime(100)); michael@0: } michael@0: michael@0: TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) { michael@0: EXPECT_TRUE(this->table_->IsPrime(2)); michael@0: EXPECT_TRUE(this->table_->IsPrime(3)); michael@0: EXPECT_TRUE(this->table_->IsPrime(5)); michael@0: EXPECT_TRUE(this->table_->IsPrime(7)); michael@0: EXPECT_TRUE(this->table_->IsPrime(11)); michael@0: EXPECT_TRUE(this->table_->IsPrime(131)); michael@0: } michael@0: michael@0: TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) { michael@0: EXPECT_EQ(2, this->table_->GetNextPrime(0)); michael@0: EXPECT_EQ(3, this->table_->GetNextPrime(2)); michael@0: EXPECT_EQ(5, this->table_->GetNextPrime(3)); michael@0: EXPECT_EQ(7, this->table_->GetNextPrime(5)); michael@0: EXPECT_EQ(11, this->table_->GetNextPrime(7)); michael@0: EXPECT_EQ(131, this->table_->GetNextPrime(128)); michael@0: } michael@0: michael@0: // Type-parameterized tests involve one extra step: you have to michael@0: // enumerate the tests you defined: michael@0: REGISTER_TYPED_TEST_CASE_P( michael@0: PrimeTableTest2, // The first argument is the test case name. michael@0: // The rest of the arguments are the test names. michael@0: ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime); michael@0: michael@0: // At this point the test pattern is done. However, you don't have michael@0: // any real test yet as you haven't said which types you want to run michael@0: // the tests with. michael@0: michael@0: // To turn the abstract test pattern into real tests, you instantiate michael@0: // it with a list of types. Usually the test pattern will be defined michael@0: // in a .h file, and anyone can #include and instantiate it. You can michael@0: // even instantiate it more than once in the same program. To tell michael@0: // different instances apart, you give each of them a name, which will michael@0: // become part of the test case name and can be used in test filters. michael@0: michael@0: // The list of types we want to test. Note that it doesn't have to be michael@0: // defined at the time we write the TYPED_TEST_P()s. michael@0: typedef Types michael@0: PrimeTableImplementations; michael@0: INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated, // Instance name michael@0: PrimeTableTest2, // Test case name michael@0: PrimeTableImplementations); // Type list michael@0: michael@0: #endif // GTEST_HAS_TYPED_TEST_P