From e1cdce5f761ed0fb04be0aea4d1b9138e9dbc1f7 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 6 Feb 2009 01:09:43 +0000 Subject: [PATCH] Implements action Throw(exception). --- include/gmock/gmock-generated-actions.h | 10 ++++++++++ include/gmock/gmock-generated-actions.h.pump | 10 ++++++++++ test/gmock-generated-actions_test.cc | 21 ++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/gmock/gmock-generated-actions.h b/include/gmock/gmock-generated-actions.h index 5188b917..b67fa982 100644 --- a/include/gmock/gmock-generated-actions.h +++ b/include/gmock/gmock-generated-actions.h @@ -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_ diff --git a/include/gmock/gmock-generated-actions.h.pump b/include/gmock/gmock-generated-actions.h.pump index 7b042ff8..0c403eb6 100644 --- a/include/gmock/gmock-generated-actions.h.pump +++ b/include/gmock/gmock-generated-actions.h.pump @@ -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_ diff --git a/test/gmock-generated-actions_test.cc b/test/gmock-generated-actions_test.cc index aee2c905..0d5260a2 100644 --- a/test/gmock-generated-actions_test.cc +++ b/test/gmock-generated-actions_test.cc @@ -1310,5 +1310,26 @@ TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) { EXPECT_EQ(55, a.Perform(empty)); } +#if GTEST_HAS_EXCEPTIONS + +TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { + const Action a = Throw('a'); + EXPECT_THROW(a.Perform(make_tuple(0)), char); +} + +class MyException {}; + +TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { + const Action a = Throw(MyException()); + EXPECT_THROW(a.Perform(make_tuple('0')), MyException); +} + +TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { + const Action a = Throw(MyException()); + EXPECT_THROW(a.Perform(make_tuple()), MyException); +} + +#endif // GTEST_HAS_EXCEPTIONS + } // namespace gmock_generated_actions_test } // namespace testing