diff options
| -rw-r--r-- | googlemock/include/gmock/gmock-generated-matchers.h | 4 | ||||
| -rw-r--r-- | googlemock/include/gmock/gmock-generated-matchers.h.pump | 3 | ||||
| -rw-r--r-- | googletest/include/gtest/gtest-typed-test.h | 104 | ||||
| -rw-r--r-- | googletest/include/gtest/internal/gtest-internal.h | 84 | ||||
| -rw-r--r-- | googletest/src/gtest.cc | 7 | ||||
| -rw-r--r-- | googletest/test/googletest-output-test-golden-lin.txt | 60 | ||||
| -rw-r--r-- | googletest/test/googletest-output-test_.cc | 46 | ||||
| -rw-r--r-- | googletest/test/gtest-typed-test_test.cc | 82 | 
8 files changed, 322 insertions, 68 deletions
| diff --git a/googlemock/include/gmock/gmock-generated-matchers.h b/googlemock/include/gmock/gmock-generated-matchers.h index 0d46d23b..41d53048 100644 --- a/googlemock/include/gmock/gmock-generated-matchers.h +++ b/googlemock/include/gmock/gmock-generated-matchers.h @@ -1381,8 +1381,8 @@ AnyOf(M1 m1, M2 m2, M3 m3, M4 m4, M5 m5, M6 m6, M7 m7, M8 m8, M9 m9, M10 m10) {  // ================  //  // To learn more about using these macros, please search for 'MATCHER' -// on https://github.com/google/googletest/blob/master/googlemock/docs/ -// CookBook.md +// on +// https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md  #define MATCHER(name, description)\    class name##Matcher {\ diff --git a/googlemock/include/gmock/gmock-generated-matchers.h.pump b/googlemock/include/gmock/gmock-generated-matchers.h.pump index 9fe0fd7a..1a59fede 100644 --- a/googlemock/include/gmock/gmock-generated-matchers.h.pump +++ b/googlemock/include/gmock/gmock-generated-matchers.h.pump @@ -592,7 +592,8 @@ $$   // show up in the generated code.  // ================  //  // To learn more about using these macros, please search for 'MATCHER' -// on https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md +// on +// https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md  $range i 0..n  $for i diff --git a/googletest/include/gtest/gtest-typed-test.h b/googletest/include/gtest/gtest-typed-test.h index 61d8907e..29b08599 100644 --- a/googletest/include/gtest/gtest-typed-test.h +++ b/googletest/include/gtest/gtest-typed-test.h @@ -83,6 +83,24 @@ TYPED_TEST(FooTest, DoesBlah) {  TYPED_TEST(FooTest, HasPropertyA) { ... } +// TYPED_TEST_CASE takes an optional third argument which allows to specify a +// class that generates custom test name suffixes based on the type. This should +// be a class which has a static template function GetName(int index) returning +// a string for each type. The provided integer index equals the index of the +// type in the provided type list. In many cases the index can be ignored. +// +// For example: +//   class MyTypeNames { +//    public: +//     template <typename T> +//     static std::string GetName(int) { +//       if (std::is_same<T, char>()) return "char"; +//       if (std::is_same<T, int>()) return "int"; +//       if (std::is_same<T, unsigned int>()) return "unsigned_int"; +//     } +//   }; +//   TYPED_TEST_CASE(FooTest, MyTypes, MyTypeNames); +  #endif  // 0  // Type-parameterized tests are abstract test patterns parameterized @@ -144,6 +162,11 @@ INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);  // If the type list contains only one type, you can write that type  // directly without Types<...>:  //   INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int); +// +// Similar to the optional argument of TYPED_TEST_CASE above, +// INSTANTIATE_TEST_CASE_P takes an optional fourth argument which allows to +// generate custom names. +//   INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes, MyTypeNames);  #endif  // 0 @@ -160,32 +183,45 @@ INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);  // given test case.  # define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_ +// Expands to the name of the typedef for the NameGenerator, responsible for +// creating the suffixes of the name. +#define GTEST_NAME_GENERATOR_(TestCaseName) \ +  gtest_type_params_##TestCaseName##_NameGenerator +  // The 'Types' template argument below must have spaces around it  // since some compilers may choke on '>>' when passing a template  // instance (e.g. Types<int>) -# define TYPED_TEST_CASE(CaseName, Types) \ -  typedef ::testing::internal::TypeList< Types >::type \ -      GTEST_TYPE_PARAMS_(CaseName) - -# define TYPED_TEST(CaseName, TestName) \ -  template <typename gtest_TypeParam_> \ -  class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ -      : public CaseName<gtest_TypeParam_> { \ -   private: \ -    typedef CaseName<gtest_TypeParam_> TestFixture; \ -    typedef gtest_TypeParam_ TypeParam; \ -    virtual void TestBody(); \ -  }; \ -  bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \ -      ::testing::internal::TypeParameterizedTest< \ -          CaseName, \ -          ::testing::internal::TemplateSel< \ -              GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \ -          GTEST_TYPE_PARAMS_(CaseName)>::Register(\ -              "", ::testing::internal::CodeLocation(__FILE__, __LINE__), \ -              #CaseName, #TestName, 0); \ -  template <typename gtest_TypeParam_> \ -  void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody() +# define TYPED_TEST_CASE(CaseName, Types, ...)                             \ +  typedef ::testing::internal::TypeList< Types >::type GTEST_TYPE_PARAMS_( \ +      CaseName);                                                           \ +  typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type    \ +      GTEST_NAME_GENERATOR_(CaseName) + +# define TYPED_TEST(CaseName, TestName)                                       \ +  template <typename gtest_TypeParam_>                                        \ +  class GTEST_TEST_CLASS_NAME_(CaseName, TestName)                            \ +      : public CaseName<gtest_TypeParam_> {                                   \ +   private:                                                                   \ +    typedef CaseName<gtest_TypeParam_> TestFixture;                           \ +    typedef gtest_TypeParam_ TypeParam;                                       \ +    virtual void TestBody();                                                  \ +  };                                                                          \ +  bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ =  \ +      ::testing::internal::TypeParameterizedTest<                             \ +          CaseName,                                                           \ +          ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName,   \ +                                                                  TestName)>, \ +          GTEST_TYPE_PARAMS_(                                                 \ +              CaseName)>::Register("",                                        \ +                                   ::testing::internal::CodeLocation(         \ +                                       __FILE__, __LINE__),                   \ +                                   #CaseName, #TestName, 0,                   \ +                                   ::testing::internal::GenerateNames<        \ +                                       GTEST_NAME_GENERATOR_(CaseName),       \ +                                       GTEST_TYPE_PARAMS_(CaseName)>());      \ +  template <typename gtest_TypeParam_>                                        \ +  void GTEST_TEST_CLASS_NAME_(CaseName,                                       \ +                              TestName)<gtest_TypeParam_>::TestBody()  #endif  // GTEST_HAS_TYPED_TEST @@ -250,15 +286,19 @@ INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);  // The 'Types' template argument below must have spaces around it  // since some compilers may choke on '>>' when passing a template  // instance (e.g. Types<int>) -# define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \ -  bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \ -      ::testing::internal::TypeParameterizedTestCase<CaseName, \ -          GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \ -          ::testing::internal::TypeList< Types >::type>::Register(\ -              #Prefix, \ -              ::testing::internal::CodeLocation(__FILE__, __LINE__), \ -              >EST_TYPED_TEST_CASE_P_STATE_(CaseName), \ -              #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName)) +# define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types, ...)      \ +  bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ =              \ +      ::testing::internal::TypeParameterizedTestCase<                     \ +          CaseName, GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_,     \ +          ::testing::internal::TypeList< Types >::type>::                 \ +          Register(#Prefix,                                               \ +                   ::testing::internal::CodeLocation(__FILE__, __LINE__), \ +                   >EST_TYPED_TEST_CASE_P_STATE_(CaseName), #CaseName,  \ +                   GTEST_REGISTERED_TEST_NAMES_(CaseName),                \ +                   ::testing::internal::GenerateNames<                    \ +                       ::testing::internal::NameGeneratorSelector<        \ +                           __VA_ARGS__>::type,                            \ +                       ::testing::internal::TypeList< Types >::type>())  #endif  // GTEST_HAS_TYPED_TEST_P diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 9593a45a..b762f61f 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -606,6 +606,37 @@ inline std::string GetPrefixUntilComma(const char* str) {  void SplitString(const ::std::string& str, char delimiter,                   ::std::vector< ::std::string>* dest); +// The default argument to the template below for the case when the user does +// not provide a name generator. +struct DefaultNameGenerator { +  template <typename T> +  static std::string GetName(int i) { +    return StreamableToString(i); +  } +}; + +template <typename Provided = DefaultNameGenerator> +struct NameGeneratorSelector { +  typedef Provided type; +}; + +template <typename NameGenerator> +void GenerateNamesRecursively(Types0, std::vector<std::string>*, int) {} + +template <typename NameGenerator, typename Types> +void GenerateNamesRecursively(Types, std::vector<std::string>* result, int i) { +  result->push_back(NameGenerator::template GetName<typename Types::Head>(i)); +  GenerateNamesRecursively<NameGenerator>(typename Types::Tail(), result, +                                          i + 1); +} + +template <typename NameGenerator, typename Types> +std::vector<std::string> GenerateNames() { +  std::vector<std::string> result; +  GenerateNamesRecursively<NameGenerator>(Types(), &result, 0); +  return result; +} +  // TypeParameterizedTest<Fixture, TestSel, Types>::Register()  // registers a list of type-parameterized tests with Google Test.  The  // return value is insignificant - we just need to return something @@ -620,10 +651,10 @@ class TypeParameterizedTest {    // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase,    // Types).  Valid values for 'index' are [0, N - 1] where N is the    // length of Types. -  static bool Register(const char* prefix, -                       const CodeLocation& code_location, -                       const char* case_name, const char* test_names, -                       int index) { +  static bool Register(const char* prefix, const CodeLocation& code_location, +                       const char* case_name, const char* test_names, int index, +                       const std::vector<std::string>& type_names = +                           GenerateNames<DefaultNameGenerator, Types>()) {      typedef typename Types::Head Type;      typedef Fixture<Type> FixtureClass;      typedef typename GTEST_BIND_(TestSel, Type) TestClass; @@ -631,20 +662,23 @@ class TypeParameterizedTest {      // First, registers the first type-parameterized test in the type      // list.      MakeAndRegisterTestInfo( -        (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + "/" -         + StreamableToString(index)).c_str(), +        (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + +         "/" + type_names[index]) +            .c_str(),          StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(),          GetTypeName<Type>().c_str(),          NULL,  // No value parameter. -        code_location, -        GetTypeId<FixtureClass>(), -        TestClass::SetUpTestCase, -        TestClass::TearDownTestCase, -        new TestFactoryImpl<TestClass>); +        code_location, GetTypeId<FixtureClass>(), TestClass::SetUpTestCase, +        TestClass::TearDownTestCase, new TestFactoryImpl<TestClass>);      // Next, recurses (at compile time) with the tail of the type list. -    return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail> -        ::Register(prefix, code_location, case_name, test_names, index + 1); +    return TypeParameterizedTest<Fixture, TestSel, +                                 typename Types::Tail>::Register(prefix, +                                                                 code_location, +                                                                 case_name, +                                                                 test_names, +                                                                 index + 1, +                                                                 type_names);    }  }; @@ -654,7 +688,9 @@ class TypeParameterizedTest<Fixture, TestSel, Types0> {   public:    static bool Register(const char* /*prefix*/, const CodeLocation&,                         const char* /*case_name*/, const char* /*test_names*/, -                       int /*index*/) { +                       int /*index*/, +                       const std::vector<std::string>& = +                           std::vector<std::string>() /*type_names*/) {      return true;    }  }; @@ -667,8 +703,10 @@ template <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types>  class TypeParameterizedTestCase {   public:    static bool Register(const char* prefix, CodeLocation code_location, -                       const TypedTestCasePState* state, -                       const char* case_name, const char* test_names) { +                       const TypedTestCasePState* state, const char* case_name, +                       const char* test_names, +                       const std::vector<std::string>& type_names = +                           GenerateNames<DefaultNameGenerator, Types>()) {      std::string test_name = StripTrailingSpaces(          GetPrefixUntilComma(test_names));      if (!state->TestExists(test_name)) { @@ -685,12 +723,14 @@ class TypeParameterizedTestCase {      // First, register the first test in 'Test' for each type in 'Types'.      TypeParameterizedTest<Fixture, Head, Types>::Register( -        prefix, test_location, case_name, test_names, 0); +        prefix, test_location, case_name, test_names, 0, type_names);      // Next, recurses (at compile time) with the tail of the test list. -    return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types> -        ::Register(prefix, code_location, state, -                   case_name, SkipComma(test_names)); +    return TypeParameterizedTestCase<Fixture, typename Tests::Tail, +                                     Types>::Register(prefix, code_location, +                                                      state, case_name, +                                                      SkipComma(test_names), +                                                      type_names);    }  }; @@ -700,7 +740,9 @@ class TypeParameterizedTestCase<Fixture, Templates0, Types> {   public:    static bool Register(const char* /*prefix*/, const CodeLocation&,                         const TypedTestCasePState* /*state*/, -                       const char* /*case_name*/, const char* /*test_names*/) { +                       const char* /*case_name*/, const char* /*test_names*/, +                       const std::vector<std::string>& = +                           std::vector<std::string>() /*type_names*/) {      return true;    }  }; diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index f476044b..97966377 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -459,8 +459,6 @@ FilePath GetCurrentExecutableName() {  // Returns the output format, or "" for normal printed output.  std::string UnitTestOptions::GetOutputFormat() {    const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); -  if (gtest_output_flag == NULL) return std::string(""); -    const char* const colon = strchr(gtest_output_flag, ':');    return (colon == NULL) ?        std::string(gtest_output_flag) : @@ -469,11 +467,8 @@ std::string UnitTestOptions::GetOutputFormat() {  // Returns the name of the requested output file, or the default if none  // was explicitly specified. -// FIXME Remove GetAbsolutePathToOutputFile checking gtest_output_flag == NULL  std::string UnitTestOptions::GetAbsolutePathToOutputFile() {    const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); -  if (gtest_output_flag == NULL) -    return "";    std::string format = GetOutputFormat();    if (format.empty()) @@ -3505,7 +3500,7 @@ class XmlUnitTestResultPrinter : public EmptyTestEventListener {  // Creates a new XmlUnitTestResultPrinter.  XmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file)      : output_file_(output_file) { -  if (output_file_.c_str() == NULL || output_file_.empty()) { +  if (output_file_.empty()) {      GTEST_LOG_(FATAL) << "XML output file may not be null";    }  } diff --git a/googletest/test/googletest-output-test-golden-lin.txt b/googletest/test/googletest-output-test-golden-lin.txt index b0c51898..7711cdc6 100644 --- a/googletest/test/googletest-output-test-golden-lin.txt +++ b/googletest/test/googletest-output-test-golden-lin.txt @@ -12,7 +12,7 @@ Expected equality of these values:    3  Stack trace: (omitted) -[0;32m[==========] [mRunning 68 tests from 30 test cases. +[0;32m[==========] [mRunning 76 tests from 34 test cases.  [0;32m[----------] [mGlobal test environment set-up.  FooEnvironment::SetUp() called.  BarEnvironment::SetUp() called. @@ -606,6 +606,24 @@ Expected failure  Stack trace: (omitted)  [0;31m[  FAILED  ] [mTypedTest/0.Failure, where TypeParam = int +[0;32m[----------] [m2 tests from TypedTestWithNames/char_0, where TypeParam = char +[0;32m[ RUN      ] [mTypedTestWithNames/char_0.Success +[0;32m[       OK ] [mTypedTestWithNames/char_0.Success +[0;32m[ RUN      ] [mTypedTestWithNames/char_0.Failure +googletest-output-test_.cc:#: Failure +Failed +Stack trace: (omitted) + +[0;31m[  FAILED  ] [mTypedTestWithNames/char_0.Failure, where TypeParam = char +[0;32m[----------] [m2 tests from TypedTestWithNames/int_1, where TypeParam = int +[0;32m[ RUN      ] [mTypedTestWithNames/int_1.Success +[0;32m[       OK ] [mTypedTestWithNames/int_1.Success +[0;32m[ RUN      ] [mTypedTestWithNames/int_1.Failure +googletest-output-test_.cc:#: Failure +Failed +Stack trace: (omitted) + +[0;31m[  FAILED  ] [mTypedTestWithNames/int_1.Failure, where TypeParam = int  [0;32m[----------] [m2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char  [0;32m[ RUN      ] [mUnsigned/TypedTestP/0.Success  [0;32m[       OK ] [mUnsigned/TypedTestP/0.Success @@ -634,6 +652,34 @@ Expected failure  Stack trace: (omitted)  [0;31m[  FAILED  ] [mUnsigned/TypedTestP/1.Failure, where TypeParam = unsigned int +[0;32m[----------] [m2 tests from UnsignedCustomName/TypedTestP/unsigned_char_0, where TypeParam = unsigned char +[0;32m[ RUN      ] [mUnsignedCustomName/TypedTestP/unsigned_char_0.Success +[0;32m[       OK ] [mUnsignedCustomName/TypedTestP/unsigned_char_0.Success +[0;32m[ RUN      ] [mUnsignedCustomName/TypedTestP/unsigned_char_0.Failure +googletest-output-test_.cc:#: Failure +Expected equality of these values: +  1U +    Which is: 1 +  TypeParam() +    Which is: '\0' +Expected failure +Stack trace: (omitted) + +[0;31m[  FAILED  ] [mUnsignedCustomName/TypedTestP/unsigned_char_0.Failure, where TypeParam = unsigned char +[0;32m[----------] [m2 tests from UnsignedCustomName/TypedTestP/unsigned_int_1, where TypeParam = unsigned int +[0;32m[ RUN      ] [mUnsignedCustomName/TypedTestP/unsigned_int_1.Success +[0;32m[       OK ] [mUnsignedCustomName/TypedTestP/unsigned_int_1.Success +[0;32m[ RUN      ] [mUnsignedCustomName/TypedTestP/unsigned_int_1.Failure +googletest-output-test_.cc:#: Failure +Expected equality of these values: +  1U +    Which is: 1 +  TypeParam() +    Which is: 0 +Expected failure +Stack trace: (omitted) + +[0;31m[  FAILED  ] [mUnsignedCustomName/TypedTestP/unsigned_int_1.Failure, where TypeParam = unsigned int  [0;32m[----------] [m4 tests from ExpectFailureTest  [0;32m[ RUN      ] [mExpectFailureTest.ExpectFatalFailure  (expecting 1 failure) @@ -860,9 +906,9 @@ Failed  Expected fatal failure.  Stack trace: (omitted) -[0;32m[==========] [m68 tests from 30 test cases ran. -[0;32m[  PASSED  ] [m22 tests. -[0;31m[  FAILED  ] [m46 tests, listed below: +[0;32m[==========] [m76 tests from 34 test cases ran. +[0;32m[  PASSED  ] [m26 tests. +[0;31m[  FAILED  ] [m50 tests, listed below:  [0;31m[  FAILED  ] [mNonfatalFailureTest.EscapesStringOperands  [0;31m[  FAILED  ] [mNonfatalFailureTest.DiffForLongStrings  [0;31m[  FAILED  ] [mFatalFailureTest.FatalFailureInSubroutine @@ -898,8 +944,12 @@ Stack trace: (omitted)  [0;31m[  FAILED  ] [mExpectFatalFailureTest.FailsWhenStatementReturns  [0;31m[  FAILED  ] [mExpectFatalFailureTest.FailsWhenStatementThrows  [0;31m[  FAILED  ] [mTypedTest/0.Failure, where TypeParam = int +[0;31m[  FAILED  ] [mTypedTestWithNames/char_0.Failure, where TypeParam = char +[0;31m[  FAILED  ] [mTypedTestWithNames/int_1.Failure, where TypeParam = int  [0;31m[  FAILED  ] [mUnsigned/TypedTestP/0.Failure, where TypeParam = unsigned char  [0;31m[  FAILED  ] [mUnsigned/TypedTestP/1.Failure, where TypeParam = unsigned int +[0;31m[  FAILED  ] [mUnsignedCustomName/TypedTestP/unsigned_char_0.Failure, where TypeParam = unsigned char +[0;31m[  FAILED  ] [mUnsignedCustomName/TypedTestP/unsigned_int_1.Failure, where TypeParam = unsigned int  [0;31m[  FAILED  ] [mExpectFailureTest.ExpectFatalFailure  [0;31m[  FAILED  ] [mExpectFailureTest.ExpectNonFatalFailure  [0;31m[  FAILED  ] [mExpectFailureTest.ExpectFatalFailureOnAllThreads @@ -910,7 +960,7 @@ Stack trace: (omitted)  [0;31m[  FAILED  ] [mPrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2  [0;31m[  FAILED  ] [mPrintingStrings/ParamTest.Failure/a, where GetParam() = "a" -46 FAILED TESTS +50 FAILED TESTS  [0;33m  YOU HAVE 1 DISABLED TEST  [mNote: Google Test filter = FatalFailureTest.*:LoggingTest.* diff --git a/googletest/test/googletest-output-test_.cc b/googletest/test/googletest-output-test_.cc index 3860cf45..180630e2 100644 --- a/googletest/test/googletest-output-test_.cc +++ b/googletest/test/googletest-output-test_.cc @@ -39,6 +39,10 @@  #include <stdlib.h> +#if _MSC_VER +  GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) +#endif  // . _MSC_VER     +  #if GTEST_IS_THREADSAFE  using testing::ScopedFakeTestPartResultReporter;  using testing::TestPartResultArray; @@ -801,6 +805,28 @@ TYPED_TEST(TypedTest, Failure) {    EXPECT_EQ(1, TypeParam()) << "Expected failure";  } +typedef testing::Types<char, int> TypesForTestWithNames; + +template <typename T> +class TypedTestWithNames : public testing::Test {}; + +class TypedTestNames { + public: +  template <typename T> +  static std::string GetName(int i) { +    if (testing::internal::IsSame<T, char>::value) +      return std::string("char_") + ::testing::PrintToString(i); +    if (testing::internal::IsSame<T, int>::value) +      return std::string("int_") + ::testing::PrintToString(i); +  } +}; + +TYPED_TEST_CASE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames); + +TYPED_TEST(TypedTestWithNames, Success) {} + +TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); } +  #endif  // GTEST_HAS_TYPED_TEST  // This #ifdef block tests the output of type-parameterized tests. @@ -825,6 +851,22 @@ REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);  typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;  INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes); +class TypedTestPNames { + public: +  template <typename T> +  static std::string GetName(int i) { +    if (testing::internal::IsSame<T, unsigned char>::value) { +      return std::string("unsigned_char_") + ::testing::PrintToString(i); +    } +    if (testing::internal::IsSame<T, unsigned int>::value) { +      return std::string("unsigned_int_") + ::testing::PrintToString(i); +    } +  } +}; + +INSTANTIATE_TYPED_TEST_CASE_P(UnsignedCustomName, TypedTestP, UnsignedTypes, +                              TypedTestPNames); +  #endif  // GTEST_HAS_TYPED_TEST_P  #if GTEST_HAS_DEATH_TEST @@ -1060,6 +1102,8 @@ int main(int argc, char **argv) {    // are registered, and torn down in the reverse order.    testing::AddGlobalTestEnvironment(new FooEnvironment);    testing::AddGlobalTestEnvironment(new BarEnvironment); - +#if _MSC_VER +  GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4127 +#endif  // . _MSC_VER        return RunAllTests();  } diff --git a/googletest/test/gtest-typed-test_test.cc b/googletest/test/gtest-typed-test_test.cc index eddb52bf..c38bb94a 100644 --- a/googletest/test/gtest-typed-test_test.cc +++ b/googletest/test/gtest-typed-test_test.cc @@ -35,6 +35,10 @@  #include "gtest/gtest.h" +#if _MSC_VER +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) +#endif  //  _MSC_VER +  using testing::Test;  // Used for testing that SetUpTestCase()/TearDownTestCase(), fixture @@ -165,6 +169,40 @@ TYPED_TEST(NumericTest, DefaultIsZero) {  }  // namespace library1 +// Tests that custom names work. +template <typename T> +class TypedTestWithNames : public Test {}; + +class TypedTestNames { + public: +  template <typename T> +  static std::string GetName(int i) { +    if (testing::internal::IsSame<T, char>::value) { +      return std::string("char_") + ::testing::PrintToString(i); +    } +    if (testing::internal::IsSame<T, int>::value) { +      return std::string("int_") + ::testing::PrintToString(i); +    } +  } +}; + +TYPED_TEST_CASE(TypedTestWithNames, TwoTypes, TypedTestNames); + +TYPED_TEST(TypedTestWithNames, TestCaseName) { +  if (testing::internal::IsSame<TypeParam, char>::value) { +    EXPECT_STREQ(::testing::UnitTest::GetInstance() +                     ->current_test_info() +                     ->test_case_name(), +                 "TypedTestWithNames/char_0"); +  } +  if (testing::internal::IsSame<TypeParam, int>::value) { +    EXPECT_STREQ(::testing::UnitTest::GetInstance() +                     ->current_test_info() +                     ->test_case_name(), +                 "TypedTestWithNames/int_1"); +  } +} +  #endif  // GTEST_HAS_TYPED_TEST  // This #ifdef block tests type-parameterized tests. @@ -265,6 +303,46 @@ REGISTER_TYPED_TEST_CASE_P(DerivedTest,  typedef Types<short, long> MyTwoTypes;  INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes); +// Tests that custom names work with type parametrized tests. We reuse the +// TwoTypes from above here. +template <typename T> +class TypeParametrizedTestWithNames : public Test {}; + +TYPED_TEST_CASE_P(TypeParametrizedTestWithNames); + +TYPED_TEST_P(TypeParametrizedTestWithNames, TestCaseName) { +  if (testing::internal::IsSame<TypeParam, char>::value) { +    EXPECT_STREQ(::testing::UnitTest::GetInstance() +                     ->current_test_info() +                     ->test_case_name(), +                 "CustomName/TypeParametrizedTestWithNames/p_char_0"); +  } +  if (testing::internal::IsSame<TypeParam, int>::value) { +    EXPECT_STREQ(::testing::UnitTest::GetInstance() +                     ->current_test_info() +                     ->test_case_name(), +                 "CustomName/TypeParametrizedTestWithNames/p_int_1"); +  } +} + +REGISTER_TYPED_TEST_CASE_P(TypeParametrizedTestWithNames, TestCaseName); + +class TypeParametrizedTestNames { + public: +  template <typename T> +  static std::string GetName(int i) { +    if (testing::internal::IsSame<T, char>::value) { +      return std::string("p_char_") + ::testing::PrintToString(i); +    } +    if (testing::internal::IsSame<T, int>::value) { +      return std::string("p_int_") + ::testing::PrintToString(i); +    } +  } +}; + +INSTANTIATE_TYPED_TEST_CASE_P(CustomName, TypeParametrizedTestWithNames, +                              TwoTypes, TypeParametrizedTestNames); +  // Tests that multiple TYPED_TEST_CASE_P's can be defined in the same  // translation unit. @@ -376,4 +454,8 @@ INSTANTIATE_TYPED_TEST_CASE_P(My, TrimmedTest, TrimTypes);  // must be defined). This dummy test keeps gtest_main linked in.  TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {} +#if _MSC_VER +GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4127 +#endif                             //  _MSC_VER +  #endif  // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P) | 
