summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--googlemock/docs/pump_manual.md21
-rw-r--r--googlemock/include/gmock/gmock-matchers.h2
-rw-r--r--googletest/include/gtest/gtest-printers.h35
-rw-r--r--googletest/include/gtest/gtest.h48
-rw-r--r--googletest/include/gtest/internal/gtest-internal.h36
-rw-r--r--googletest/include/gtest/internal/gtest-port.h14
-rw-r--r--googletest/src/gtest.cc31
-rw-r--r--googletest/test/googletest-printers-test.cc25
-rw-r--r--googletest/test/gtest_unittest.cc19
9 files changed, 145 insertions, 86 deletions
diff --git a/googlemock/docs/pump_manual.md b/googlemock/docs/pump_manual.md
index 10b3c5f..cdf7c57 100644
--- a/googlemock/docs/pump_manual.md
+++ b/googlemock/docs/pump_manual.md
@@ -6,18 +6,15 @@ Template and macro libraries often need to define many classes, functions, or
macros that vary only (or almost only) in the number of arguments they take.
It's a lot of repetitive, mechanical, and error-prone work.
-Variadic templates and variadic macros can alleviate the problem. However, while
-both are being considered by the C++ committee, neither is in the standard yet
-or widely supported by compilers. Thus they are often not a good choice,
-especially when your code needs to be portable. And their capabilities are still
-limited.
-
-As a result, authors of such libraries often have to write scripts to generate
-their implementation. However, our experience is that it's tedious to write such
-scripts, which tend to reflect the structure of the generated code poorly and
-are often hard to read and edit. For example, a small change needed in the
-generated code may require some non-intuitive, non-trivial changes in the
-script. This is especially painful when experimenting with the code.
+Our experience is that it's tedious to write custom scripts, which tend to
+reflect the structure of the generated code poorly and are often hard to read
+and edit. For example, a small change needed in the generated code may require
+some non-intuitive, non-trivial changes in the script. This is especially
+painful when experimenting with the code.
+
+This script may be useful for generating meta code, for example a series of
+macros of FOO1, FOO2, etc. Nevertheless, please make it your last resort
+technique by favouring C++ template metaprogramming or variadic macros.
# Our Solution
diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h
index b8ec24d..e71570b 100644
--- a/googlemock/include/gmock/gmock-matchers.h
+++ b/googlemock/include/gmock/gmock-matchers.h
@@ -1323,7 +1323,7 @@ class PredicateFormatterFromMatcher {
<< "Expected: ";
matcher.DescribeTo(&ss);
- // Rerun the matcher to "PrintAndExain" the failure.
+ // Rerun the matcher to "PrintAndExplain" the failure.
StringMatchResultListener listener;
if (MatchPrintAndExplain(x, matcher, &listener)) {
ss << "\n The matcher failed on the initial attempt; but passed when "
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h
index 808d319..56a0545 100644
--- a/googletest/include/gtest/gtest-printers.h
+++ b/googletest/include/gtest/gtest-printers.h
@@ -133,7 +133,7 @@ GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
// nor PrintTo().
enum TypeKind {
kProtobuf, // a protobuf type
- kConvertibleToInteger, // a type implicitly convertible to std::intmax_t
+ kConvertibleToInteger, // a type implicitly convertible to BiggestInt
// (e.g. a named or unnamed enum type)
#if GTEST_HAS_ABSL
kConvertibleToStringView, // a type implicitly convertible to
@@ -179,14 +179,14 @@ template <typename T>
class TypeWithoutFormatter<T, kConvertibleToInteger> {
public:
// Since T has no << operator or PrintTo() but can be implicitly
- // converted to the maximum width integer, we print it as a std::intmax_t.
+ // converted to BiggestInt, we print it as a BiggestInt.
//
// Most likely T is an enum type (either named or unnamed), in which
- // case printing it as an integer is the desired behavior. In case
+ // case printing it as an integer is the desired behavior. In case
// T is not an enum, printing it as an integer is the best we can do
// given that it has no user-defined printer.
static void PrintValue(const T& value, ::std::ostream* os) {
- const std::intmax_t kBigInt = value;
+ const internal::BiggestInt kBigInt = value;
*os << kBigInt;
}
};
@@ -204,10 +204,10 @@ class TypeWithoutFormatter<T, kConvertibleToStringView> {
};
#endif
-// Prints the given value to the given ostream. If the value is a
+// Prints the given value to the given ostream. If the value is a
// protocol message, its debug string is printed; if it's an enum or
-// of a type implicitly convertible to std::intmax_t, it's printed as an
-// integer; otherwise the bytes in the value are printed. This is
+// of a type implicitly convertible to BiggestInt, it's printed as an
+// integer; otherwise the bytes in the value are printed. This is
// what UniversalPrinter<T>::Print() does when it knows nothing about
// type T and T has neither << operator nor PrintTo().
//
@@ -231,18 +231,19 @@ class TypeWithoutFormatter<T, kConvertibleToStringView> {
template <typename Char, typename CharTraits, typename T>
::std::basic_ostream<Char, CharTraits>& operator<<(
::std::basic_ostream<Char, CharTraits>& os, const T& x) {
- TypeWithoutFormatter<
- T, (internal::IsAProtocolMessage<T>::value
- ? kProtobuf
- : std::is_convertible<const T&, std::intmax_t>::value
- ? kConvertibleToInteger
- :
+ TypeWithoutFormatter<T, (internal::IsAProtocolMessage<T>::value
+ ? kProtobuf
+ : std::is_convertible<
+ const T&, internal::BiggestInt>::value
+ ? kConvertibleToInteger
+ :
#if GTEST_HAS_ABSL
- std::is_convertible<const T&, absl::string_view>::value
- ? kConvertibleToStringView
- :
+ std::is_convertible<
+ const T&, absl::string_view>::value
+ ? kConvertibleToStringView
+ :
#endif
- kOtherType)>::PrintValue(x, &os);
+ kOtherType)>::PrintValue(x, &os);
return os;
}
diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h
index 0e1c4f7..8eda6ea 100644
--- a/googletest/include/gtest/gtest.h
+++ b/googletest/include/gtest/gtest.h
@@ -1531,11 +1531,13 @@ AssertionResult CmpHelperEQ(const char* lhs_expression,
return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs);
}
-// With this overloaded version, we allow anonymous enums to be used in
-// {ASSERT|EXPECT}_EQ as anonymous enums can be implicitly cast to integers.
+// With this overloaded version, we allow anonymous enums to be used
+// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
+// can be implicitly cast to BiggestInt.
GTEST_API_ AssertionResult CmpHelperEQ(const char* lhs_expression,
const char* rhs_expression,
- std::intmax_t lhs, std::intmax_t rhs);
+ BiggestInt lhs,
+ BiggestInt rhs);
class EqHelper {
public:
@@ -1552,14 +1554,16 @@ class EqHelper {
return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
}
- // With this overloaded version, we allow anonymous enums to be used in
- // {ASSERT|EXPECT}_EQ as anonymous enums can be implicitly cast to integers.
+ // With this overloaded version, we allow anonymous enums to be used
+ // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
+ // enums can be implicitly cast to BiggestInt.
//
// Even though its body looks the same as the above version, we
// cannot merge the two, as it will make anonymous enums unhappy.
static AssertionResult Compare(const char* lhs_expression,
- const char* rhs_expression, std::intmax_t lhs,
- std::intmax_t rhs) {
+ const char* rhs_expression,
+ BiggestInt lhs,
+ BiggestInt rhs) {
return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);
}
@@ -1592,24 +1596,24 @@ AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2,
// of similar code.
//
// For each templatized helper function, we also define an overloaded
-// version for std::intmax_t in order to reduce code bloat and allow
-// anonymous enums to be used with {ASSERT|EXPECT}_??.
+// version for BiggestInt in order to reduce code bloat and allow
+// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
+// with gcc 4.
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
-#define GTEST_IMPL_CMP_HELPER_(op_name, op) \
- template <typename T1, typename T2> \
- AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
- const T1& val1, const T2& val2) { \
- if (val1 op val2) { \
- return AssertionSuccess(); \
- } else { \
- return CmpHelperOpFailure(expr1, expr2, val1, val2, #op); \
- } \
- } \
- GTEST_API_ AssertionResult CmpHelper##op_name( \
- const char* expr1, const char* expr2, std::intmax_t val1, \
- std::intmax_t val2)
+#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
+template <typename T1, typename T2>\
+AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
+ const T1& val1, const T2& val2) {\
+ if (val1 op val2) {\
+ return AssertionSuccess();\
+ } else {\
+ return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\
+ }\
+}\
+GTEST_API_ AssertionResult CmpHelper##op_name(\
+ const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h
index edf0afa..f9da489 100644
--- a/googletest/include/gtest/internal/gtest-internal.h
+++ b/googletest/include/gtest/internal/gtest-internal.h
@@ -825,6 +825,16 @@ struct GTEST_API_ ConstCharPtr {
const char* value;
};
+// Helper for declaring std::string within 'if' statement
+// in pre C++17 build environment.
+struct GTEST_API_ TrueWithString {
+ TrueWithString() = default;
+ explicit TrueWithString(const char* str) : value(str) {}
+ explicit TrueWithString(const std::string& str) : value(str) {}
+ explicit operator bool() const { return true; }
+ std::string value;
+};
+
// A simple Linear Congruential Generator for generating random
// numbers with a uniform distribution. Unlike rand() and srand(), it
// doesn't use global state (and therefore can't interfere with user
@@ -1284,19 +1294,39 @@ constexpr bool InstantiateTypedTestCase_P_IsDeprecated() { return true; }
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
fail(gtest_msg.value)
+#if GTEST_HAS_EXCEPTIONS
+
+#define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \
+ catch (std::exception const& e) { \
+ gtest_msg.value = ( \
+ "it throws std::exception-derived exception with description: \"" \
+ ); \
+ gtest_msg.value += e.what(); \
+ gtest_msg.value += "\"."; \
+ goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
+ }
+
+#else // GTEST_HAS_EXCEPTIONS
+
+#define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_()
+
+#endif // GTEST_HAS_EXCEPTIONS
+
#define GTEST_TEST_NO_THROW_(statement, fail) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
- if (::testing::internal::AlwaysTrue()) { \
+ if (::testing::internal::TrueWithString gtest_msg{}) { \
try { \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
} \
+ GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \
catch (...) { \
+ gtest_msg.value = "it throws."; \
goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
} \
} else \
GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \
- fail("Expected: " #statement " doesn't throw an exception.\n" \
- " Actual: it throws.")
+ fail(("Expected: " #statement " doesn't throw an exception.\n" \
+ " Actual: " + gtest_msg.value).c_str())
#define GTEST_TEST_ANY_THROW_(statement, fail) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h
index 6bb8f51..90be25e 100644
--- a/googletest/include/gtest/internal/gtest-port.h
+++ b/googletest/include/gtest/internal/gtest-port.h
@@ -225,6 +225,7 @@
// TypeWithSize - maps an integer to a int type.
// Int32, UInt32, Int64, UInt64, TimeInMillis
// - integers of known sizes.
+// BiggestInt - the biggest signed integer type.
//
// Command-line utilities:
// GTEST_DECLARE_*() - declares a flag.
@@ -1875,9 +1876,12 @@ GTEST_API_ size_t GetThreadCount();
#if GTEST_OS_WINDOWS
# define GTEST_PATH_SEP_ "\\"
# define GTEST_HAS_ALT_PATH_SEP_ 1
+// The biggest signed integer type the compiler supports.
+typedef __int64 BiggestInt;
#else
# define GTEST_PATH_SEP_ "/"
# define GTEST_HAS_ALT_PATH_SEP_ 0
+typedef long long BiggestInt; // NOLINT
#endif // GTEST_OS_WINDOWS
// Utilities for char.
@@ -2080,6 +2084,16 @@ GTEST_DISABLE_MSC_DEPRECATED_POP_()
# define GTEST_SNPRINTF_ snprintf
#endif
+// The maximum number a BiggestInt can represent. This definition
+// works no matter BiggestInt is represented in one's complement or
+// two's complement.
+//
+// We cannot rely on numeric_limits in STL, as __int64 and long long
+// are not part of standard C++ and numeric_limits doesn't need to be
+// defined for them.
+const BiggestInt kMaxBiggestInt =
+ ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
+
// This template class serves as a compile-time function from size to
// type. It maps a size in bytes to a primitive type with that
// size. e.g.
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc
index 7c1c9ed..8afb070 100644
--- a/googletest/src/gtest.cc
+++ b/googletest/src/gtest.cc
@@ -1442,8 +1442,9 @@ namespace internal {
// The helper function for {ASSERT|EXPECT}_EQ with int or enum
// arguments.
AssertionResult CmpHelperEQ(const char* lhs_expression,
- const char* rhs_expression, std::intmax_t lhs,
- std::intmax_t rhs) {
+ const char* rhs_expression,
+ BiggestInt lhs,
+ BiggestInt rhs) {
if (lhs == rhs) {
return AssertionSuccess();
}
@@ -1456,20 +1457,20 @@ AssertionResult CmpHelperEQ(const char* lhs_expression,
}
// A macro for implementing the helper functions needed to implement
-// ASSERT_?? and EXPECT_?? with integer or enum arguments. It is here
+// ASSERT_?? and EXPECT_?? with integer or enum arguments. It is here
// just to avoid copy-and-paste of similar code.
-#define GTEST_IMPL_CMP_HELPER_(op_name, op) \
- AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
- std::intmax_t val1, std::intmax_t val2) { \
- if (val1 op val2) { \
- return AssertionSuccess(); \
- } else { \
- return AssertionFailure() \
- << "Expected: (" << expr1 << ") " #op " (" << expr2 \
- << "), actual: " << FormatForComparisonFailureMessage(val1, val2) \
- << " vs " << FormatForComparisonFailureMessage(val2, val1); \
- } \
- }
+#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
+AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
+ BiggestInt val1, BiggestInt val2) {\
+ if (val1 op val2) {\
+ return AssertionSuccess();\
+ } else {\
+ return AssertionFailure() \
+ << "Expected: (" << expr1 << ") " #op " (" << expr2\
+ << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
+ << " vs " << FormatForComparisonFailureMessage(val2, val1);\
+ }\
+}
// Implements the helper function for {ASSERT|EXPECT}_NE with int or
// enum arguments.
diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc
index cebd6ac..58be7d1 100644
--- a/googletest/test/googletest-printers-test.cc
+++ b/googletest/test/googletest-printers-test.cc
@@ -83,12 +83,10 @@ void PrintTo(EnumWithPrintTo e, std::ostream* os) {
*os << (e == kEWPT1 ? "kEWPT1" : "invalid");
}
-// A class implicitly convertible to std::intmax_t.
-class IntMaxConvertible {
+// A class implicitly convertible to BiggestInt.
+class BiggestIntConvertible {
public:
- constexpr operator std::intmax_t() const { // NOLINT(runtime/explicit)
- return 42;
- }
+ operator ::testing::internal::BiggestInt() const { return 42; }
};
// A user-defined unprintable class template in the global namespace.
@@ -269,10 +267,10 @@ TEST(PrintEnumTest, EnumWithPrintTo) {
EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
}
-// Tests printing a class implicitly convertible to std::intmax_t.
+// Tests printing a class implicitly convertible to BiggestInt.
-TEST(PrintClassTest, IntMaxConvertible) {
- EXPECT_EQ("42", Print(IntMaxConvertible()));
+TEST(PrintClassTest, BiggestIntConvertible) {
+ EXPECT_EQ("42", Print(BiggestIntConvertible()));
}
// Tests printing various char types.
@@ -527,9 +525,10 @@ TEST(PrintPointerTest, NonMemberFunctionPointer) {
// standard disallows casting between pointers to functions and
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// this limitation.
- EXPECT_EQ(PrintPointer(reinterpret_cast<const void*>(
- reinterpret_cast<std::intptr_t>(&MyFunction))),
- Print(&MyFunction));
+ EXPECT_EQ(
+ PrintPointer(reinterpret_cast<const void*>(
+ reinterpret_cast<internal::BiggestInt>(&MyFunction))),
+ Print(&MyFunction));
int (*p)(bool) = NULL; // NOLINT
EXPECT_EQ("NULL", Print(p));
}
@@ -1121,8 +1120,8 @@ TEST(PrintReferenceTest, HandlesFunctionPointer) {
// standard disallows casting between pointers to functions and
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// this limitation.
- const std::string fp_string = PrintPointer(
- reinterpret_cast<const void*>(reinterpret_cast<std::intptr_t>(fp)));
+ const std::string fp_string = PrintPointer(reinterpret_cast<const void*>(
+ reinterpret_cast<internal::BiggestInt>(fp)));
EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
PrintByRef(fp));
}
diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc
index ae8834d..74379ab 100644
--- a/googletest/test/gtest_unittest.cc
+++ b/googletest/test/gtest_unittest.cc
@@ -55,11 +55,11 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused.
}
+#include <limits.h> // For INT_MAX.
#include <stdlib.h>
#include <string.h>
#include <time.h>
-#include <limits>
#include <map>
#include <ostream>
#include <type_traits>
@@ -3348,6 +3348,9 @@ TEST_F(SingleEvaluationTest, OtherCases) {
void ThrowAnInteger() {
throw 1;
}
+void ThrowRuntimeError(const char* what) {
+ throw std::runtime_error(what);
+}
// Tests that assertion arguments are evaluated exactly once.
TEST_F(SingleEvaluationTest, ExceptionTests) {
@@ -3827,6 +3830,11 @@ TEST(AssertionTest, ASSERT_NO_THROW) {
EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()),
"Expected: ThrowAnInteger() doesn't throw an exception."
"\n Actual: it throws.");
+ EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowRuntimeError("A description")),
+ "Expected: ThrowRuntimeError(\"A description\") "
+ "doesn't throw an exception.\n "
+ "Actual: it throws std::exception-derived exception "
+ "with description: \"A description\".");
}
// Tests ASSERT_ANY_THROW.
@@ -3952,11 +3960,11 @@ enum {
// On Linux, kCaseB and kCaseA have the same value when truncated to
// int size. We want to test whether this will confuse the
// assertions.
- kCaseB = (std::numeric_limits<std::intmax_t>::max)(),
+ kCaseB = testing::internal::kMaxBiggestInt,
# else
- kCaseB = (std::numeric_limits<int>::max)(),
+ kCaseB = INT_MAX,
# endif // GTEST_OS_LINUX
@@ -4564,6 +4572,11 @@ TEST(ExpectTest, EXPECT_NO_THROW) {
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()),
"Expected: ThrowAnInteger() doesn't throw an "
"exception.\n Actual: it throws.");
+ EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowRuntimeError("A description")),
+ "Expected: ThrowRuntimeError(\"A description\") "
+ "doesn't throw an exception.\n "
+ "Actual: it throws std::exception-derived exception "
+ "with description: \"A description\".");
}
// Tests EXPECT_ANY_THROW.