From 097f64e98693500d44d022a781745e562aa492ad Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Thu, 9 Feb 2023 15:29:01 +0800 Subject: [PATCH 1/2] Revert "Fix gmock_output_test when using MSVC" This reverts commit 0a3b403fe037ff80daa1826ae99eed41e94dea05. --- googlemock/test/gmock_output_test_.cc | 9 ++------- googlemock/test/gmock_output_test_golden.txt | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/googlemock/test/gmock_output_test_.cc b/googlemock/test/gmock_output_test_.cc index ca5a646f3a..af4eaa9ebd 100644 --- a/googlemock/test/gmock_output_test_.cc +++ b/googlemock/test/gmock_output_test_.cc @@ -33,7 +33,6 @@ #include #include -#include #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -255,16 +254,12 @@ TEST_F(GMockOutputTest, CatchesLeakedMocks) { } MATCHER_P2(IsPair, first, second, "") { - return Value(std::get<0>(arg), first) && Value(std::get<1>(arg), second); + return Value(arg.first, first) && Value(arg.second, second); } TEST_F(GMockOutputTest, PrintsMatcher) { const testing::Matcher m1 = Ge(48); - // Explicitly using std::tuple instead of std::pair due to differences between - // MSVC and other compilers. std::pair is printed as - // "struct std::pair" when using MSVC vs "std::pair" with - // other compilers. - EXPECT_THAT((std::tuple(42, true)), IsPair(m1, true)); + EXPECT_THAT((std::pair(42, true)), IsPair(m1, true)); } void TestCatchesLeakedMocksInAdHocTests() { diff --git a/googlemock/test/gmock_output_test_golden.txt b/googlemock/test/gmock_output_test_golden.txt index ac2a5e2192..467fa20ef3 100644 --- a/googlemock/test/gmock_output_test_golden.txt +++ b/googlemock/test/gmock_output_test_golden.txt @@ -290,9 +290,9 @@ Stack trace: [ OK ] GMockOutputTest.CatchesLeakedMocks [ RUN ] GMockOutputTest.PrintsMatcher FILE:#: Failure -Value of: (std::tuple(42, true)) +Value of: (std::pair(42, true)) Expected: is pair (first: is >= 48, second: true) - Actual: (42, true) + Actual: (42, true) (of type std::pair) [ FAILED ] GMockOutputTest.PrintsMatcher [ FAILED ] GMockOutputTest.UnexpectedCall [ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction From 6f1c4b3d7b139c7217698bf4115be9f40785f661 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Thu, 24 Nov 2022 23:54:19 +0800 Subject: [PATCH 2/2] Fixes the test gmock_output_test.py with MSVC For MSVC, gmock_output_test.py output struct std::pair, for GCC, it's output std::pair, it's not the same, my intention is getting these to be same by removing struct for MSVC's outptu, and strip redundant space for GCC. As a by-product, ``` #ifdef _MSC_VER #define ERROR_DESC "class std::runtime_error" #else #define ERROR_DESC "std::runtime_error" #endif ``` can be simplified to ``` #define ERROR_DESC "std::runtime_error" ``` Signed-off-by: Yonggang Luo --- googlemock/test/gmock_output_test_golden.txt | 2 +- .../include/gtest/internal/gtest-type-util.h | 30 +++++++++++++++++++ googletest/test/gtest_unittest.cc | 4 --- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/googlemock/test/gmock_output_test_golden.txt b/googlemock/test/gmock_output_test_golden.txt index 467fa20ef3..9a7adf6af7 100644 --- a/googlemock/test/gmock_output_test_golden.txt +++ b/googlemock/test/gmock_output_test_golden.txt @@ -292,7 +292,7 @@ Stack trace: FILE:#: Failure Value of: (std::pair(42, true)) Expected: is pair (first: is >= 48, second: true) - Actual: (42, true) (of type std::pair) + Actual: (42, true) (of type std::pair) [ FAILED ] GMockOutputTest.PrintsMatcher [ FAILED ] GMockOutputTest.UnexpectedCall [ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction diff --git a/googletest/include/gtest/internal/gtest-type-util.h b/googletest/include/gtest/internal/gtest-type-util.h index 17a470b626..b23ad559f8 100644 --- a/googletest/include/gtest/internal/gtest-type-util.h +++ b/googletest/include/gtest/internal/gtest-type-util.h @@ -67,6 +67,22 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) { s.erase(strlen("std"), end - strlen("std")); } } + + /* Strip redundant spaces in typename to match MSVC */ + /* For example, std::pair -> std::pair */ + static const char to_search[] = ", "; + static const char replace_str[] = ","; + size_t pos = 0; + while (true) { + // Get the next occurrence from the current position + pos = s.find(to_search, pos); + if (pos == std::string::npos) { + break; + } + // Replace this occurrence of substring + s.replace(pos, strlen(to_search), replace_str); + pos += strlen(replace_str); + } return s; } @@ -85,6 +101,20 @@ inline std::string GetTypeName(const std::type_info& type) { const std::string name_str(status == 0 ? readable_name : name); free(readable_name); return CanonicalizeForStdLibVersioning(name_str); +#elif defined(_MSC_VER) + // Strip struct and class due to differences between + // MSVC and other compilers. std::pair is printed as + // "struct std::pair" when using MSVC vs "std::pair" with + // other compilers. + std::string s = name; + // Only strip the leading "struct " and "class ", so uses rfind == 0 to + // ensure that + if (s.rfind("struct ", 0) == 0) { + s = s.substr(strlen("struct ")); + } else if (s.rfind("class ", 0) == 0) { + s = s.substr(strlen("class ")); + } + return s; #else return name; #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index ecffa3765a..180c3abe66 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -3314,11 +3314,7 @@ TEST_F(SingleEvaluationTest, OtherCases) { #if GTEST_HAS_RTTI -#ifdef _MSC_VER -#define ERROR_DESC "class std::runtime_error" -#else #define ERROR_DESC "std::runtime_error" -#endif #else // GTEST_HAS_RTTI