zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file implements some actions that depend on gmock-generated-actions.h. |
| 35 | |
| 36 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ |
| 37 | #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ |
| 38 | |
| 39 | #include <gmock/gmock-generated-actions.h> |
| 40 | |
| 41 | namespace testing { |
| 42 | namespace internal { |
| 43 | |
| 44 | // Implements the Invoke(f) action. The template argument |
| 45 | // FunctionImpl is the implementation type of f, which can be either a |
| 46 | // function pointer or a functor. Invoke(f) can be used as an |
| 47 | // Action<F> as long as f's type is compatible with F (i.e. f can be |
| 48 | // assigned to a tr1::function<F>). |
| 49 | template <typename FunctionImpl> |
| 50 | class InvokeAction { |
| 51 | public: |
| 52 | // The c'tor makes a copy of function_impl (either a function |
| 53 | // pointer or a functor). |
| 54 | explicit InvokeAction(FunctionImpl function_impl) |
| 55 | : function_impl_(function_impl) {} |
| 56 | |
| 57 | template <typename Result, typename ArgumentTuple> |
| 58 | Result Perform(const ArgumentTuple& args) { |
| 59 | return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); |
| 60 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 61 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 62 | private: |
| 63 | FunctionImpl function_impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 64 | |
| 65 | GTEST_DISALLOW_ASSIGN_(InvokeAction); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // Implements the Invoke(object_ptr, &Class::Method) action. |
| 69 | template <class Class, typename MethodPtr> |
| 70 | class InvokeMethodAction { |
| 71 | public: |
| 72 | InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) |
| 73 | : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} |
| 74 | |
| 75 | template <typename Result, typename ArgumentTuple> |
| 76 | Result Perform(const ArgumentTuple& args) const { |
| 77 | return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( |
| 78 | obj_ptr_, method_ptr_, args); |
| 79 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 80 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 81 | private: |
| 82 | Class* const obj_ptr_; |
| 83 | const MethodPtr method_ptr_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 84 | |
| 85 | GTEST_DISALLOW_ASSIGN_(InvokeMethodAction); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace internal |
| 89 | |
| 90 | // Various overloads for Invoke(). |
| 91 | |
| 92 | // Creates an action that invokes 'function_impl' with the mock |
| 93 | // function's arguments. |
| 94 | template <typename FunctionImpl> |
| 95 | PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( |
| 96 | FunctionImpl function_impl) { |
| 97 | return MakePolymorphicAction( |
| 98 | internal::InvokeAction<FunctionImpl>(function_impl)); |
| 99 | } |
| 100 | |
| 101 | // Creates an action that invokes the given method on the given object |
| 102 | // with the mock function's arguments. |
| 103 | template <class Class, typename MethodPtr> |
| 104 | PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( |
| 105 | Class* obj_ptr, MethodPtr method_ptr) { |
| 106 | return MakePolymorphicAction( |
| 107 | internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); |
| 108 | } |
| 109 | |
| 110 | // WithoutArgs(inner_action) can be used in a mock function with a |
| 111 | // non-empty argument list to perform inner_action, which takes no |
| 112 | // argument. In other words, it adapts an action accepting no |
| 113 | // argument to one that accepts (and ignores) arguments. |
| 114 | template <typename InnerAction> |
| 115 | inline internal::WithArgsAction<InnerAction> |
| 116 | WithoutArgs(const InnerAction& action) { |
| 117 | return internal::WithArgsAction<InnerAction>(action); |
| 118 | } |
| 119 | |
| 120 | // WithArg<k>(an_action) creates an action that passes the k-th |
| 121 | // (0-based) argument of the mock function to an_action and performs |
| 122 | // it. It adapts an action accepting one argument to one that accepts |
| 123 | // multiple arguments. For convenience, we also provide |
| 124 | // WithArgs<k>(an_action) (defined below) as a synonym. |
| 125 | template <int k, typename InnerAction> |
| 126 | inline internal::WithArgsAction<InnerAction, k> |
| 127 | WithArg(const InnerAction& action) { |
| 128 | return internal::WithArgsAction<InnerAction, k>(action); |
| 129 | } |
| 130 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 131 | // The ACTION*() macros trigger warning C4100 (unreferenced formal |
| 132 | // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in |
| 133 | // the macro definition, as the warnings are generated when the macro |
| 134 | // is expanded and macro expansion cannot contain #pragma. Therefore |
| 135 | // we suppress them here. |
| 136 | #ifdef _MSC_VER |
| 137 | #pragma warning(push) |
| 138 | #pragma warning(disable:4100) |
| 139 | #endif |
| 140 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 141 | // Action ReturnArg<k>() returns the k-th argument of the mock function. |
| 142 | ACTION_TEMPLATE(ReturnArg, |
| 143 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 144 | AND_0_VALUE_PARAMS()) { |
| 145 | return std::tr1::get<k>(args); |
| 146 | } |
| 147 | |
| 148 | // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the |
| 149 | // mock function to *pointer. |
| 150 | ACTION_TEMPLATE(SaveArg, |
| 151 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 152 | AND_1_VALUE_PARAMS(pointer)) { |
| 153 | *pointer = ::std::tr1::get<k>(args); |
| 154 | } |
| 155 | |
| 156 | // Action SetArgReferee<k>(value) assigns 'value' to the variable |
| 157 | // referenced by the k-th (0-based) argument of the mock function. |
| 158 | ACTION_TEMPLATE(SetArgReferee, |
| 159 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 160 | AND_1_VALUE_PARAMS(value)) { |
| 161 | typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type; |
| 162 | // Ensures that argument #k is a reference. If you get a compiler |
| 163 | // error on the next line, you are using SetArgReferee<k>(value) in |
| 164 | // a mock function whose k-th (0-based) argument is not a reference. |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 165 | GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 166 | SetArgReferee_must_be_used_with_a_reference_argument); |
| 167 | ::std::tr1::get<k>(args) = value; |
| 168 | } |
| 169 | |
| 170 | // Action SetArrayArgument<k>(first, last) copies the elements in |
| 171 | // source range [first, last) to the array pointed to by the k-th |
| 172 | // (0-based) argument, which can be either a pointer or an |
| 173 | // iterator. The action does not take ownership of the elements in the |
| 174 | // source range. |
| 175 | ACTION_TEMPLATE(SetArrayArgument, |
| 176 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 177 | AND_2_VALUE_PARAMS(first, last)) { |
| 178 | // Microsoft compiler deprecates ::std::copy, so we want to suppress warning |
| 179 | // 4996 (Function call with parameters that may be unsafe) there. |
| 180 | #ifdef _MSC_VER |
| 181 | #pragma warning(push) // Saves the current warning state. |
| 182 | #pragma warning(disable:4996) // Temporarily disables warning 4996. |
| 183 | #endif |
| 184 | ::std::copy(first, last, ::std::tr1::get<k>(args)); |
| 185 | #ifdef _MSC_VER |
| 186 | #pragma warning(pop) // Restores the warning state. |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock |
| 191 | // function. |
| 192 | ACTION_TEMPLATE(DeleteArg, |
| 193 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 194 | AND_0_VALUE_PARAMS()) { |
| 195 | delete ::std::tr1::get<k>(args); |
| 196 | } |
| 197 | |
| 198 | // Action Throw(exception) can be used in a mock function of any type |
| 199 | // to throw the given exception. Any copyable value can be thrown. |
| 200 | #if GTEST_HAS_EXCEPTIONS |
vladlosev | e2e8ba4 | 2010-05-13 18:16:03 +0000 | [diff] [blame^] | 201 | |
| 202 | // Suppresses the 'unreachable code' warning that VC generates in opt modes. |
| 203 | #ifdef _MSC_VER |
| 204 | #pragma warning(push) // Saves the current warning state. |
| 205 | #pragma warning(disable:4702) // Temporarily disables warning 4702. |
| 206 | #endif |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 207 | ACTION_P(Throw, exception) { throw exception; } |
vladlosev | e2e8ba4 | 2010-05-13 18:16:03 +0000 | [diff] [blame^] | 208 | #ifdef _MSC_VER |
| 209 | #pragma warning(pop) // Restores the warning state. |
| 210 | #endif |
| 211 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 212 | #endif // GTEST_HAS_EXCEPTIONS |
| 213 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 214 | #ifdef _MSC_VER |
| 215 | #pragma warning(pop) |
| 216 | #endif |
| 217 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 218 | } // namespace testing |
| 219 | |
| 220 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ |