Adds two actions: SaveArg and SetArgReferee.

This commit is contained in:
zhanyong.wan 2009-02-19 22:38:27 +00:00
parent 38ca64dd5f
commit 7f4c2c0f95
3 changed files with 177 additions and 31 deletions

View File

@ -998,51 +998,57 @@ class ActionHelper {
public:
static Result Perform(Impl* impl, const ::std::tr1::tuple<>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, ExcessiveArg(), ExcessiveArg(),
return impl->template gmock_PerformImpl<>(args, ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0>
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), ExcessiveArg(),
return impl->template gmock_PerformImpl<A0>(args, get<0>(args),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1>
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
return impl->template gmock_PerformImpl<A0, A1>(args, get<0>(args),
get<1>(args), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
ExcessiveArg());
}
template <typename A0, typename A1, typename A2>
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2>(args, get<0>(args),
get<1>(args), get<2>(args), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3>
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2,
A3>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2, A3>(args, get<0>(args),
get<1>(args), get<2>(args), get<3>(args), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3, typename A4>
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2, A3,
A4>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), get<4>(args), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2, A3, A4>(args,
get<0>(args), get<1>(args), get<2>(args), get<3>(args), get<4>(args),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3, typename A4,
@ -1050,9 +1056,10 @@ class ActionHelper {
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2, A3, A4,
A5>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), get<4>(args), get<5>(args), ExcessiveArg(),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2, A3, A4, A5>(args,
get<0>(args), get<1>(args), get<2>(args), get<3>(args), get<4>(args),
get<5>(args), ExcessiveArg(), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3, typename A4,
@ -1060,9 +1067,10 @@ class ActionHelper {
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2, A3, A4,
A5, A6>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), get<4>(args), get<5>(args), get<6>(args),
ExcessiveArg(), ExcessiveArg(), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2, A3, A4, A5, A6>(args,
get<0>(args), get<1>(args), get<2>(args), get<3>(args), get<4>(args),
get<5>(args), get<6>(args), ExcessiveArg(), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3, typename A4,
@ -1070,9 +1078,10 @@ class ActionHelper {
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2, A3, A4,
A5, A6, A7>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), get<4>(args), get<5>(args), get<6>(args),
get<7>(args), ExcessiveArg(), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2, A3, A4, A5, A6,
A7>(args, get<0>(args), get<1>(args), get<2>(args), get<3>(args),
get<4>(args), get<5>(args), get<6>(args), get<7>(args), ExcessiveArg(),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3, typename A4,
@ -1080,9 +1089,10 @@ class ActionHelper {
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2, A3, A4,
A5, A6, A7, A8>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), get<4>(args), get<5>(args), get<6>(args),
get<7>(args), get<8>(args), ExcessiveArg());
return impl->template gmock_PerformImpl<A0, A1, A2, A3, A4, A5, A6, A7,
A8>(args, get<0>(args), get<1>(args), get<2>(args), get<3>(args),
get<4>(args), get<5>(args), get<6>(args), get<7>(args), get<8>(args),
ExcessiveArg());
}
template <typename A0, typename A1, typename A2, typename A3, typename A4,
@ -1090,9 +1100,10 @@ class ActionHelper {
static Result Perform(Impl* impl, const ::std::tr1::tuple<A0, A1, A2, A3, A4,
A5, A6, A7, A8, A9>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, get<0>(args), get<1>(args),
get<2>(args), get<3>(args), get<4>(args), get<5>(args), get<6>(args),
get<7>(args), get<8>(args), get<9>(args));
return impl->template gmock_PerformImpl<A0, A1, A2, A3, A4, A5, A6, A7, A8,
A9>(args, get<0>(args), get<1>(args), get<2>(args), get<3>(args),
get<4>(args), get<5>(args), get<6>(args), get<7>(args), get<8>(args),
get<9>(args));
}
};
@ -2303,8 +2314,44 @@ DoAll(Action1 a1, Action2 a2, Action3 a3, Action4 a4, Action5 a5, Action6 a6,
arg4_type arg4, arg5_type arg5, arg6_type arg6, arg7_type arg7, \
arg8_type arg8, arg9_type arg9) const
// TODO(wan@google.com): move the following to a different .h file
// such that we don't have to run 'pump' every time the code is
// updated.
namespace testing {
namespace internal {
// Saves argument #0 to where the pointer points.
ACTION_P(SaveArg0, pointer) { *pointer = arg0; }
// Assigns 'value' to the variable referenced by argument #0.
ACTION_P(SetArg0Referee, value) {
// Ensures that argument #0 is a reference. If you get a compiler
// error on the next line, you are using SetArgReferee<k>(value) in
// a mock function whose k-th (0-based) argument is not a reference.
GMOCK_COMPILE_ASSERT_(internal::is_reference<arg0_type>::value,
SetArgReferee_must_be_used_with_a_reference_argument);
arg0 = value;
}
} // namespace internal
// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
// mock function to *pointer.
template <int k, typename Pointer>
inline internal::WithArgsAction<internal::SaveArg0ActionP<Pointer>, k>
SaveArg(const Pointer& pointer) {
return WithArg<k>(internal::SaveArg0(pointer));
}
// Action SetArgReferee<k>(value) assigns 'value' to the variable
// referenced by the k-th (0-based) argument of the mock function.
template <int k, typename Value>
inline internal::WithArgsAction<internal::SetArg0RefereeActionP<Value>, k>
SetArgReferee(const Value& value) {
return WithArg<k>(internal::SetArg0Referee(value));
}
// Action Throw(exception) can be used in a mock function of any type
// to throw the given exception. Any copyable value can be thrown.
#if GTEST_HAS_EXCEPTIONS

View File

@ -436,7 +436,7 @@ $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]]
$template
static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) {
using ::std::tr1::get;
return impl->gmock_PerformImpl(args, $arg_list);
return impl->template gmock_PerformImpl<$As>(args, $arg_list);
}
]]
@ -774,10 +774,48 @@ $arg_types_and_names) const;\$param_field_decls
gmock_Impl<F>::gmock_PerformImpl(const args_type& args, [[]]
$arg_types_and_names) const
]]
$$ } // This meta comment fixes auto-indentation in Emacs. It won't
$$ // show up in the generated code.
// TODO(wan@google.com): move the following to a different .h file
// such that we don't have to run 'pump' every time the code is
// updated.
namespace testing {
namespace internal {
// Saves argument #0 to where the pointer points.
ACTION_P(SaveArg0, pointer) { *pointer = arg0; }
// Assigns 'value' to the variable referenced by argument #0.
ACTION_P(SetArg0Referee, value) {
// Ensures that argument #0 is a reference. If you get a compiler
// error on the next line, you are using SetArgReferee<k>(value) in
// a mock function whose k-th (0-based) argument is not a reference.
GMOCK_COMPILE_ASSERT_(internal::is_reference<arg0_type>::value,
SetArgReferee_must_be_used_with_a_reference_argument);
arg0 = value;
}
} // namespace internal
// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
// mock function to *pointer.
template <int k, typename Pointer>
inline internal::WithArgsAction<internal::SaveArg0ActionP<Pointer>, k>
SaveArg(const Pointer& pointer) {
return WithArg<k>(internal::SaveArg0(pointer));
}
// Action SetArgReferee<k>(value) assigns 'value' to the variable
// referenced by the k-th (0-based) argument of the mock function.
template <int k, typename Value>
inline internal::WithArgsAction<internal::SetArg0RefereeActionP<Value>, k>
SetArgReferee(const Value& value) {
return WithArg<k>(internal::SetArg0Referee(value));
}
// Action Throw(exception) can be used in a mock function of any type
// to throw the given exception. Any copyable value can be thrown.
#if GTEST_HAS_EXCEPTIONS

View File

@ -57,6 +57,8 @@ using testing::DoAll;
using testing::Invoke;
using testing::InvokeArgument;
using testing::Return;
using testing::SaveArg;
using testing::SetArgReferee;
using testing::SetArgumentPointee;
using testing::StaticAssertTypeEq;
using testing::Unused;
@ -1026,6 +1028,30 @@ TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
EXPECT_EQ(1, a1.Perform(make_tuple(false)));
}
// Tests that ACTION() works for arguments passed by const reference.
ACTION(ReturnAddrOfConstBoolReferenceArg) {
StaticAssertTypeEq<const bool&, arg1_type>();
return &arg1;
}
TEST(ActionMacroTest, WorksForConstReferenceArg) {
Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
const bool b = false;
EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b)));
}
// Tests that ACTION() works for arguments passed by non-const reference.
ACTION(ReturnAddrOfIntReferenceArg) {
StaticAssertTypeEq<int&, arg0_type>();
return &arg0;
}
TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
int n = 0;
EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1)));
}
// Tests that ACTION() can be used in a namespace.
namespace action_test {
ACTION(Sum) { return arg0 + arg1; }
@ -1310,6 +1336,41 @@ TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
EXPECT_EQ(55, a.Perform(empty));
}
TEST(SaveArgActionTest, WorksForSameType) {
int result = 0;
const Action<void(int n)> a1 = SaveArg<0>(&result);
a1.Perform(make_tuple(5));
EXPECT_EQ(5, result);
}
TEST(SaveArgActionTest, WorksForCompatibleType) {
int result = 0;
const Action<void(bool, char)> a1 = SaveArg<1>(&result);
a1.Perform(make_tuple(true, 'a'));
EXPECT_EQ('a', result);
}
TEST(SetArgRefereeActionTest, WorksForSameType) {
int value = 0;
const Action<void(int&)> a1 = SetArgReferee<0>(1);
a1.Perform(tuple<int&>(value));
EXPECT_EQ(1, value);
}
TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
int value = 0;
const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
a1.Perform(tuple<int, int&>(0, value));
EXPECT_EQ('a', value);
}
TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
int value = 0;
const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
EXPECT_EQ('a', value);
}
#if GTEST_HAS_EXCEPTIONS
TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {