Blame googletest/include/gtest/gtest-typed-test.h

Packit bd1cd8
// Copyright 2008 Google Inc.
Packit bd1cd8
// All Rights Reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
Packit bd1cd8
#define GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
Packit bd1cd8
Packit bd1cd8
// This header implements typed tests and type-parameterized tests.
Packit bd1cd8
Packit bd1cd8
// Typed (aka type-driven) tests repeat the same test for types in a
Packit bd1cd8
// list.  You must know which types you want to test with when writing
Packit bd1cd8
// typed tests. Here's how you do it:
Packit bd1cd8
Packit bd1cd8
#if 0
Packit bd1cd8
Packit bd1cd8
// First, define a fixture class template.  It should be parameterized
Packit bd1cd8
// by a type.  Remember to derive it from testing::Test.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class FooTest : public testing::Test {
Packit bd1cd8
 public:
Packit bd1cd8
  ...
Packit bd1cd8
  typedef std::list<T> List;
Packit bd1cd8
  static T shared_;
Packit bd1cd8
  T value_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Next, associate a list of types with the test case, which will be
Packit bd1cd8
// repeated for each type in the list.  The typedef is necessary for
Packit bd1cd8
// the macro to parse correctly.
Packit bd1cd8
typedef testing::Types<char, int, unsigned int> MyTypes;
Packit bd1cd8
TYPED_TEST_CASE(FooTest, MyTypes);
Packit bd1cd8
Packit bd1cd8
// If the type list contains only one type, you can write that type
Packit bd1cd8
// directly without Types<...>:
Packit bd1cd8
//   TYPED_TEST_CASE(FooTest, int);
Packit bd1cd8
Packit bd1cd8
// Then, use TYPED_TEST() instead of TEST_F() to define as many typed
Packit bd1cd8
// tests for this test case as you want.
Packit bd1cd8
TYPED_TEST(FooTest, DoesBlah) {
Packit bd1cd8
  // Inside a test, refer to TypeParam to get the type parameter.
Packit bd1cd8
  // Since we are inside a derived class template, C++ requires use to
Packit bd1cd8
  // visit the members of FooTest via 'this'.
Packit bd1cd8
  TypeParam n = this->value_;
Packit bd1cd8
Packit bd1cd8
  // To visit static members of the fixture, add the TestFixture::
Packit bd1cd8
  // prefix.
Packit bd1cd8
  n += TestFixture::shared_;
Packit bd1cd8
Packit bd1cd8
  // To refer to typedefs in the fixture, add the "typename
Packit bd1cd8
  // TestFixture::" prefix.
Packit bd1cd8
  typename TestFixture::List values;
Packit bd1cd8
  values.push_back(n);
Packit bd1cd8
  ...
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TYPED_TEST(FooTest, HasPropertyA) { ... }
Packit bd1cd8
Packit bd1cd8
#endif  // 0
Packit bd1cd8
Packit bd1cd8
// Type-parameterized tests are abstract test patterns parameterized
Packit bd1cd8
// by a type.  Compared with typed tests, type-parameterized tests
Packit bd1cd8
// allow you to define the test pattern without knowing what the type
Packit bd1cd8
// parameters are.  The defined pattern can be instantiated with
Packit bd1cd8
// different types any number of times, in any number of translation
Packit bd1cd8
// units.
Packit bd1cd8
//
Packit bd1cd8
// If you are designing an interface or concept, you can define a
Packit bd1cd8
// suite of type-parameterized tests to verify properties that any
Packit bd1cd8
// valid implementation of the interface/concept should have.  Then,
Packit bd1cd8
// each implementation can easily instantiate the test suite to verify
Packit bd1cd8
// that it conforms to the requirements, without having to write
Packit bd1cd8
// similar tests repeatedly.  Here's an example:
Packit bd1cd8
Packit bd1cd8
#if 0
Packit bd1cd8
Packit bd1cd8
// First, define a fixture class template.  It should be parameterized
Packit bd1cd8
// by a type.  Remember to derive it from testing::Test.
Packit bd1cd8
template <typename T>
Packit bd1cd8
class FooTest : public testing::Test {
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Next, declare that you will define a type-parameterized test case
Packit bd1cd8
// (the _P suffix is for "parameterized" or "pattern", whichever you
Packit bd1cd8
// prefer):
Packit bd1cd8
TYPED_TEST_CASE_P(FooTest);
Packit bd1cd8
Packit bd1cd8
// Then, use TYPED_TEST_P() to define as many type-parameterized tests
Packit bd1cd8
// for this type-parameterized test case as you want.
Packit bd1cd8
TYPED_TEST_P(FooTest, DoesBlah) {
Packit bd1cd8
  // Inside a test, refer to TypeParam to get the type parameter.
Packit bd1cd8
  TypeParam n = 0;
Packit bd1cd8
  ...
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TYPED_TEST_P(FooTest, HasPropertyA) { ... }
Packit bd1cd8
Packit bd1cd8
// Now the tricky part: you need to register all test patterns before
Packit bd1cd8
// you can instantiate them.  The first argument of the macro is the
Packit bd1cd8
// test case name; the rest are the names of the tests in this test
Packit bd1cd8
// case.
Packit bd1cd8
REGISTER_TYPED_TEST_CASE_P(FooTest,
Packit bd1cd8
                           DoesBlah, HasPropertyA);
Packit bd1cd8
Packit bd1cd8
// Finally, you are free to instantiate the pattern with the types you
Packit bd1cd8
// want.  If you put the above code in a header file, you can #include
Packit bd1cd8
// it in multiple C++ source files and instantiate it multiple times.
Packit bd1cd8
//
Packit bd1cd8
// To distinguish different instances of the pattern, the first
Packit bd1cd8
// argument to the INSTANTIATE_* macro is a prefix that will be added
Packit bd1cd8
// to the actual test case name.  Remember to pick unique prefixes for
Packit bd1cd8
// different instances.
Packit bd1cd8
typedef testing::Types<char, int, unsigned int> MyTypes;
Packit bd1cd8
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);
Packit bd1cd8
Packit bd1cd8
// If the type list contains only one type, you can write that type
Packit bd1cd8
// directly without Types<...>:
Packit bd1cd8
//   INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int);
Packit bd1cd8
Packit bd1cd8
#endif  // 0
Packit bd1cd8
Packit bd1cd8
#include "gtest/internal/gtest-port.h"
Packit bd1cd8
#include "gtest/internal/gtest-type-util.h"
Packit bd1cd8
Packit bd1cd8
// Implements typed tests.
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_TYPED_TEST
Packit bd1cd8
Packit bd1cd8
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
Packit bd1cd8
//
Packit bd1cd8
// Expands to the name of the typedef for the type parameters of the
Packit bd1cd8
// given test case.
Packit bd1cd8
# define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_
Packit bd1cd8
Packit bd1cd8
// The 'Types' template argument below must have spaces around it
Packit bd1cd8
// since some compilers may choke on '>>' when passing a template
Packit bd1cd8
// instance (e.g. Types<int>)
Packit bd1cd8
# define TYPED_TEST_CASE(CaseName, Types) \
Packit bd1cd8
  typedef ::testing::internal::TypeList< Types >::type \
Packit bd1cd8
      GTEST_TYPE_PARAMS_(CaseName)
Packit bd1cd8
Packit bd1cd8
# define TYPED_TEST(CaseName, TestName) \
Packit bd1cd8
  template <typename gtest_TypeParam_> \
Packit bd1cd8
  class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
Packit bd1cd8
      : public CaseName<gtest_TypeParam_> { \
Packit bd1cd8
   private: \
Packit bd1cd8
    typedef CaseName<gtest_TypeParam_> TestFixture; \
Packit bd1cd8
    typedef gtest_TypeParam_ TypeParam; \
Packit bd1cd8
    virtual void TestBody(); \
Packit bd1cd8
  }; \
Packit bd1cd8
  bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \
Packit bd1cd8
      ::testing::internal::TypeParameterizedTest< \
Packit bd1cd8
          CaseName, \
Packit bd1cd8
          ::testing::internal::TemplateSel< \
Packit bd1cd8
              GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \
Packit bd1cd8
          GTEST_TYPE_PARAMS_(CaseName)>::Register(\
Packit bd1cd8
              "", ::testing::internal::CodeLocation(__FILE__, __LINE__), \
Packit bd1cd8
              #CaseName, #TestName, 0); \
Packit bd1cd8
  template <typename gtest_TypeParam_> \
Packit bd1cd8
  void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody()
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_HAS_TYPED_TEST
Packit bd1cd8
Packit bd1cd8
// Implements type-parameterized tests.
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_TYPED_TEST_P
Packit bd1cd8
Packit bd1cd8
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
Packit bd1cd8
//
Packit bd1cd8
// Expands to the namespace name that the type-parameterized tests for
Packit bd1cd8
// the given type-parameterized test case are defined in.  The exact
Packit bd1cd8
// name of the namespace is subject to change without notice.
Packit bd1cd8
# define GTEST_CASE_NAMESPACE_(TestCaseName) \
Packit bd1cd8
  gtest_case_##TestCaseName##_
Packit bd1cd8
Packit bd1cd8
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
Packit bd1cd8
//
Packit bd1cd8
// Expands to the name of the variable used to remember the names of
Packit bd1cd8
// the defined tests in the given test case.
Packit bd1cd8
# define GTEST_TYPED_TEST_CASE_P_STATE_(TestCaseName) \
Packit bd1cd8
  gtest_typed_test_case_p_state_##TestCaseName##_
Packit bd1cd8
Packit bd1cd8
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.
Packit bd1cd8
//
Packit bd1cd8
// Expands to the name of the variable used to remember the names of
Packit bd1cd8
// the registered tests in the given test case.
Packit bd1cd8
# define GTEST_REGISTERED_TEST_NAMES_(TestCaseName) \
Packit bd1cd8
  gtest_registered_test_names_##TestCaseName##_
Packit bd1cd8
Packit bd1cd8
// The variables defined in the type-parameterized test macros are
Packit bd1cd8
// static as typically these macros are used in a .h file that can be
Packit bd1cd8
// #included in multiple translation units linked together.
Packit bd1cd8
# define TYPED_TEST_CASE_P(CaseName) \
Packit bd1cd8
  static ::testing::internal::TypedTestCasePState \
Packit bd1cd8
      GTEST_TYPED_TEST_CASE_P_STATE_(CaseName)
Packit bd1cd8
Packit bd1cd8
# define TYPED_TEST_P(CaseName, TestName) \
Packit bd1cd8
  namespace GTEST_CASE_NAMESPACE_(CaseName) { \
Packit bd1cd8
  template <typename gtest_TypeParam_> \
Packit bd1cd8
  class TestName : public CaseName<gtest_TypeParam_> { \
Packit bd1cd8
   private: \
Packit bd1cd8
    typedef CaseName<gtest_TypeParam_> TestFixture; \
Packit bd1cd8
    typedef gtest_TypeParam_ TypeParam; \
Packit bd1cd8
    virtual void TestBody(); \
Packit bd1cd8
  }; \
Packit bd1cd8
  static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \
Packit bd1cd8
      GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).AddTestName(\
Packit bd1cd8
          __FILE__, __LINE__, #CaseName, #TestName); \
Packit bd1cd8
  } \
Packit bd1cd8
  template <typename gtest_TypeParam_> \
Packit bd1cd8
  void GTEST_CASE_NAMESPACE_(CaseName)::TestName<gtest_TypeParam_>::TestBody()
Packit bd1cd8
Packit bd1cd8
# define REGISTER_TYPED_TEST_CASE_P(CaseName, ...) \
Packit bd1cd8
  namespace GTEST_CASE_NAMESPACE_(CaseName) { \
Packit bd1cd8
  typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \
Packit bd1cd8
  } \
Packit bd1cd8
  static const char* const GTEST_REGISTERED_TEST_NAMES_(CaseName) = \
Packit bd1cd8
      GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).VerifyRegisteredTestNames(\
Packit bd1cd8
          __FILE__, __LINE__, #__VA_ARGS__)
Packit bd1cd8
Packit bd1cd8
// The 'Types' template argument below must have spaces around it
Packit bd1cd8
// since some compilers may choke on '>>' when passing a template
Packit bd1cd8
// instance (e.g. Types<int>)
Packit bd1cd8
# define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \
Packit bd1cd8
  bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \
Packit bd1cd8
      ::testing::internal::TypeParameterizedTestCase
Packit bd1cd8
          GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \
Packit bd1cd8
          ::testing::internal::TypeList< Types >::type>::Register(\
Packit bd1cd8
              #Prefix, \
Packit bd1cd8
              ::testing::internal::CodeLocation(__FILE__, __LINE__), \
Packit bd1cd8
              &GTEST_TYPED_TEST_CASE_P_STATE_(CaseName), \
Packit bd1cd8
              #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName))
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_HAS_TYPED_TEST_P
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_