From 387bdd551d4a88383246841ac3f70324b8d42772 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 20 Jul 2009 21:16:35 +0000 Subject: [PATCH] Makes ByRef(x) printable as a reference to x. --- include/gmock/gmock-generated-actions.h | 11 +++++++++++ include/gmock/gmock-generated-actions.h.pump | 11 +++++++++++ scripts/gmock_doctor.py | 1 - test/gmock-generated-actions_test.cc | 10 ++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/gmock/gmock-generated-actions.h b/include/gmock/gmock-generated-actions.h index fa02faaa..c0097175 100644 --- a/include/gmock/gmock-generated-actions.h +++ b/include/gmock/gmock-generated-actions.h @@ -39,6 +39,7 @@ #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ #include +#include #include namespace testing { @@ -321,6 +322,9 @@ class InvokeMethodAction { const MethodPtr method_ptr_; }; +// TODO(wan@google.com): ReferenceWrapper and ByRef() are neither +// action-specific nor variadic. Move them to a better place. + // A ReferenceWrapper object represents a reference to type T, // which can be either const or not. It can be explicitly converted // from, and implicitly converted to, a T&. Unlike a reference, @@ -341,6 +345,13 @@ class ReferenceWrapper { T* pointer_; }; +// Allows the expression ByRef(x) to be printed as a reference to x. +template +void PrintTo(const ReferenceWrapper& ref, ::std::ostream* os) { + T& value = ref; + UniversalPrinter::Print(value, os); +} + // CallableHelper has static methods for invoking "callables", // i.e. function pointers and functors. It uses overloading to // provide a uniform interface for invoking different kinds of diff --git a/include/gmock/gmock-generated-actions.h.pump b/include/gmock/gmock-generated-actions.h.pump index b5223a34..2a7e4ffd 100644 --- a/include/gmock/gmock-generated-actions.h.pump +++ b/include/gmock/gmock-generated-actions.h.pump @@ -43,6 +43,7 @@ $$}} This meta comment fixes auto-indentation in editors. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ #include +#include #include namespace testing { @@ -123,6 +124,9 @@ class InvokeMethodAction { const MethodPtr method_ptr_; }; +// TODO(wan@google.com): ReferenceWrapper and ByRef() are neither +// action-specific nor variadic. Move them to a better place. + // A ReferenceWrapper object represents a reference to type T, // which can be either const or not. It can be explicitly converted // from, and implicitly converted to, a T&. Unlike a reference, @@ -143,6 +147,13 @@ class ReferenceWrapper { T* pointer_; }; +// Allows the expression ByRef(x) to be printed as a reference to x. +template +void PrintTo(const ReferenceWrapper& ref, ::std::ostream* os) { + T& value = ref; + UniversalPrinter::Print(value, os); +} + // CallableHelper has static methods for invoking "callables", // i.e. function pointers and functors. It uses overloading to // provide a uniform interface for invoking different kinds of diff --git a/scripts/gmock_doctor.py b/scripts/gmock_doctor.py index 05e42585..7b45fa17 100755 --- a/scripts/gmock_doctor.py +++ b/scripts/gmock_doctor.py @@ -71,7 +71,6 @@ _COMMON_GMOCK_SYMBOLS = [ 'Not', 'NotNull', 'Pointee', - 'PointeeIsInitializedProto', 'Property', 'Ref', 'ResultOf', diff --git a/test/gmock-generated-actions_test.cc b/test/gmock-generated-actions_test.cc index d0b2ddc9..cf3c7891 100644 --- a/test/gmock-generated-actions_test.cc +++ b/test/gmock-generated-actions_test.cc @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -546,6 +547,15 @@ TEST(ByRefTest, ExplicitType) { // ByRef(b); } +// Tests that Google Mock prints expression ByRef(x) as a reference to x. +TEST(ByRefTest, PrintsCorrectly) { + int n = 42; + ::std::stringstream expected, actual; + testing::internal::UniversalPrinter::Print(n, &expected); + testing::internal::UniversalPrint(ByRef(n), &actual); + EXPECT_EQ(expected.str(), actual.str()); +} + // Tests InvokeArgument(...). // Tests using InvokeArgument with a nullary function.