![]() |
|
|||
File indexing completed on 2025-08-07 08:19:53
0001 // Copyright 2007, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 // 0030 // Author: wan@google.com (Zhanyong Wan) 0031 0032 // Google Mock - a framework for writing C++ mock classes. 0033 // 0034 // This file implements some actions that depend on gmock-generated-actions.h. 0035 0036 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ 0037 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ 0038 0039 #include <algorithm> 0040 0041 #include "gmock/gmock-generated-actions.h" 0042 0043 namespace testing { 0044 namespace internal { 0045 0046 // Implements the Invoke(f) action. The template argument 0047 // FunctionImpl is the implementation type of f, which can be either a 0048 // function pointer or a functor. Invoke(f) can be used as an 0049 // Action<F> as long as f's type is compatible with F (i.e. f can be 0050 // assigned to a tr1::function<F>). 0051 template <typename FunctionImpl> 0052 class InvokeAction { 0053 public: 0054 // The c'tor makes a copy of function_impl (either a function 0055 // pointer or a functor). 0056 explicit InvokeAction(FunctionImpl function_impl) 0057 : function_impl_(function_impl) {} 0058 0059 template <typename Result, typename ArgumentTuple> 0060 Result Perform(const ArgumentTuple& args) { 0061 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); 0062 } 0063 0064 private: 0065 FunctionImpl function_impl_; 0066 0067 GTEST_DISALLOW_ASSIGN_(InvokeAction); 0068 }; 0069 0070 // Implements the Invoke(object_ptr, &Class::Method) action. 0071 template <class Class, typename MethodPtr> 0072 class InvokeMethodAction { 0073 public: 0074 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) 0075 : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {} 0076 0077 template <typename Result, typename ArgumentTuple> 0078 Result Perform(const ArgumentTuple& args) const { 0079 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( 0080 obj_ptr_, method_ptr_, args); 0081 } 0082 0083 private: 0084 // The order of these members matters. Reversing the order can trigger 0085 // warning C4121 in MSVC (see 0086 // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ). 0087 const MethodPtr method_ptr_; 0088 Class* const obj_ptr_; 0089 0090 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction); 0091 }; 0092 0093 // An internal replacement for std::copy which mimics its behavior. This is 0094 // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996. 0095 // However Visual Studio 2010 and later do not honor #pragmas which disable that 0096 // warning. 0097 template<typename InputIterator, typename OutputIterator> 0098 inline OutputIterator CopyElements(InputIterator first, 0099 InputIterator last, 0100 OutputIterator output) { 0101 for (; first != last; ++first, ++output) { 0102 *output = *first; 0103 } 0104 return output; 0105 } 0106 0107 } // namespace internal 0108 0109 // Various overloads for Invoke(). 0110 0111 // Creates an action that invokes 'function_impl' with the mock 0112 // function's arguments. 0113 template <typename FunctionImpl> 0114 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( 0115 FunctionImpl function_impl) { 0116 return MakePolymorphicAction( 0117 internal::InvokeAction<FunctionImpl>(function_impl)); 0118 } 0119 0120 // Creates an action that invokes the given method on the given object 0121 // with the mock function's arguments. 0122 template <class Class, typename MethodPtr> 0123 PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( 0124 Class* obj_ptr, MethodPtr method_ptr) { 0125 return MakePolymorphicAction( 0126 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); 0127 } 0128 0129 // WithoutArgs(inner_action) can be used in a mock function with a 0130 // non-empty argument list to perform inner_action, which takes no 0131 // argument. In other words, it adapts an action accepting no 0132 // argument to one that accepts (and ignores) arguments. 0133 template <typename InnerAction> 0134 inline internal::WithArgsAction<InnerAction> 0135 WithoutArgs(const InnerAction& action) { 0136 return internal::WithArgsAction<InnerAction>(action); 0137 } 0138 0139 // WithArg<k>(an_action) creates an action that passes the k-th 0140 // (0-based) argument of the mock function to an_action and performs 0141 // it. It adapts an action accepting one argument to one that accepts 0142 // multiple arguments. For convenience, we also provide 0143 // WithArgs<k>(an_action) (defined below) as a synonym. 0144 template <int k, typename InnerAction> 0145 inline internal::WithArgsAction<InnerAction, k> 0146 WithArg(const InnerAction& action) { 0147 return internal::WithArgsAction<InnerAction, k>(action); 0148 } 0149 0150 // The ACTION*() macros trigger warning C4100 (unreferenced formal 0151 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in 0152 // the macro definition, as the warnings are generated when the macro 0153 // is expanded and macro expansion cannot contain #pragma. Therefore 0154 // we suppress them here. 0155 #ifdef _MSC_VER 0156 # pragma warning(push) 0157 # pragma warning(disable:4100) 0158 #endif 0159 0160 // Action ReturnArg<k>() returns the k-th argument of the mock function. 0161 ACTION_TEMPLATE(ReturnArg, 0162 HAS_1_TEMPLATE_PARAMS(int, k), 0163 AND_0_VALUE_PARAMS()) { 0164 return ::testing::get<k>(args); 0165 } 0166 0167 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the 0168 // mock function to *pointer. 0169 ACTION_TEMPLATE(SaveArg, 0170 HAS_1_TEMPLATE_PARAMS(int, k), 0171 AND_1_VALUE_PARAMS(pointer)) { 0172 *pointer = ::testing::get<k>(args); 0173 } 0174 0175 // Action SaveArgPointee<k>(pointer) saves the value pointed to 0176 // by the k-th (0-based) argument of the mock function to *pointer. 0177 ACTION_TEMPLATE(SaveArgPointee, 0178 HAS_1_TEMPLATE_PARAMS(int, k), 0179 AND_1_VALUE_PARAMS(pointer)) { 0180 *pointer = *::testing::get<k>(args); 0181 } 0182 0183 // Action SetArgReferee<k>(value) assigns 'value' to the variable 0184 // referenced by the k-th (0-based) argument of the mock function. 0185 ACTION_TEMPLATE(SetArgReferee, 0186 HAS_1_TEMPLATE_PARAMS(int, k), 0187 AND_1_VALUE_PARAMS(value)) { 0188 typedef typename ::testing::tuple_element<k, args_type>::type argk_type; 0189 // Ensures that argument #k is a reference. If you get a compiler 0190 // error on the next line, you are using SetArgReferee<k>(value) in 0191 // a mock function whose k-th (0-based) argument is not a reference. 0192 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, 0193 SetArgReferee_must_be_used_with_a_reference_argument); 0194 ::testing::get<k>(args) = value; 0195 } 0196 0197 // Action SetArrayArgument<k>(first, last) copies the elements in 0198 // source range [first, last) to the array pointed to by the k-th 0199 // (0-based) argument, which can be either a pointer or an 0200 // iterator. The action does not take ownership of the elements in the 0201 // source range. 0202 ACTION_TEMPLATE(SetArrayArgument, 0203 HAS_1_TEMPLATE_PARAMS(int, k), 0204 AND_2_VALUE_PARAMS(first, last)) { 0205 // Visual Studio deprecates ::std::copy, so we use our own copy in that case. 0206 #ifdef _MSC_VER 0207 internal::CopyElements(first, last, ::testing::get<k>(args)); 0208 #else 0209 ::std::copy(first, last, ::testing::get<k>(args)); 0210 #endif 0211 } 0212 0213 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock 0214 // function. 0215 ACTION_TEMPLATE(DeleteArg, 0216 HAS_1_TEMPLATE_PARAMS(int, k), 0217 AND_0_VALUE_PARAMS()) { 0218 delete ::testing::get<k>(args); 0219 } 0220 0221 // This action returns the value pointed to by 'pointer'. 0222 ACTION_P(ReturnPointee, pointer) { return *pointer; } 0223 0224 // Action Throw(exception) can be used in a mock function of any type 0225 // to throw the given exception. Any copyable value can be thrown. 0226 #if GTEST_HAS_EXCEPTIONS 0227 0228 // Suppresses the 'unreachable code' warning that VC generates in opt modes. 0229 # ifdef _MSC_VER 0230 # pragma warning(push) // Saves the current warning state. 0231 # pragma warning(disable:4702) // Temporarily disables warning 4702. 0232 # endif 0233 ACTION_P(Throw, exception) { throw exception; } 0234 # ifdef _MSC_VER 0235 # pragma warning(pop) // Restores the warning state. 0236 # endif 0237 0238 #endif // GTEST_HAS_EXCEPTIONS 0239 0240 #ifdef _MSC_VER 0241 # pragma warning(pop) 0242 #endif 0243 0244 } // namespace testing 0245 0246 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |