Implements action Throw(exception).

This commit is contained in:
zhanyong.wan 2009-02-06 01:09:43 +00:00
parent c069d7fe27
commit e1cdce5f76
3 changed files with 41 additions and 0 deletions

View File

@ -2298,4 +2298,14 @@ 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
namespace testing {
// 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
ACTION_P(Throw, exception) { throw exception; }
#endif // GTEST_HAS_EXCEPTIONS
} // namespace testing
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_

View File

@ -772,4 +772,14 @@ $arg_types_and_names) const
]]
namespace testing {
// 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
ACTION_P(Throw, exception) { throw exception; }
#endif // GTEST_HAS_EXCEPTIONS
} // namespace testing
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_

View File

@ -1310,5 +1310,26 @@ TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
EXPECT_EQ(55, a.Perform(empty));
}
#if GTEST_HAS_EXCEPTIONS
TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
const Action<void(int n)> a = Throw('a');
EXPECT_THROW(a.Perform(make_tuple(0)), char);
}
class MyException {};
TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
const Action<double(char ch)> a = Throw(MyException());
EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
}
TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
const Action<double()> a = Throw(MyException());
EXPECT_THROW(a.Perform(make_tuple()), MyException);
}
#endif // GTEST_HAS_EXCEPTIONS
} // namespace gmock_generated_actions_test
} // namespace testing