gmock-more-actions.h revision 13481:0a2c5afe5163
112837Sgabeblack@google.com// Copyright 2007, Google Inc. 212837Sgabeblack@google.com// All rights reserved. 312837Sgabeblack@google.com// 412837Sgabeblack@google.com// Redistribution and use in source and binary forms, with or without 512837Sgabeblack@google.com// modification, are permitted provided that the following conditions are 612837Sgabeblack@google.com// met: 712837Sgabeblack@google.com// 812837Sgabeblack@google.com// * Redistributions of source code must retain the above copyright 912837Sgabeblack@google.com// notice, this list of conditions and the following disclaimer. 1012837Sgabeblack@google.com// * Redistributions in binary form must reproduce the above 1112837Sgabeblack@google.com// copyright notice, this list of conditions and the following disclaimer 1212837Sgabeblack@google.com// in the documentation and/or other materials provided with the 1312837Sgabeblack@google.com// distribution. 1412837Sgabeblack@google.com// * Neither the name of Google Inc. nor the names of its 1512837Sgabeblack@google.com// contributors may be used to endorse or promote products derived from 1612837Sgabeblack@google.com// this software without specific prior written permission. 1712837Sgabeblack@google.com// 1812837Sgabeblack@google.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1912837Sgabeblack@google.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2012837Sgabeblack@google.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2112837Sgabeblack@google.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2212837Sgabeblack@google.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2312837Sgabeblack@google.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2412837Sgabeblack@google.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2512837Sgabeblack@google.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2612837Sgabeblack@google.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2712837Sgabeblack@google.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2812837Sgabeblack@google.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2912837Sgabeblack@google.com// 3013039Sgabeblack@google.com// Author: wan@google.com (Zhanyong Wan) 3113039Sgabeblack@google.com 3212837Sgabeblack@google.com// Google Mock - a framework for writing C++ mock classes. 3312989Sgabeblack@google.com// 3412986Sgabeblack@google.com// This file implements some actions that depend on gmock-generated-actions.h. 3513039Sgabeblack@google.com 3612837Sgabeblack@google.com#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ 3712837Sgabeblack@google.com#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ 3812837Sgabeblack@google.com 3912837Sgabeblack@google.com#include <algorithm> 4012837Sgabeblack@google.com 4112983Sgabeblack@google.com#include "gmock/gmock-generated-actions.h" 4212837Sgabeblack@google.com 4312983Sgabeblack@google.comnamespace testing { 4412983Sgabeblack@google.comnamespace internal { 4512983Sgabeblack@google.com 4612983Sgabeblack@google.com// Implements the Invoke(f) action. The template argument 4712983Sgabeblack@google.com// FunctionImpl is the implementation type of f, which can be either a 4812983Sgabeblack@google.com// function pointer or a functor. Invoke(f) can be used as an 4912983Sgabeblack@google.com// Action<F> as long as f's type is compatible with F (i.e. f can be 5012983Sgabeblack@google.com// assigned to a tr1::function<F>). 5112983Sgabeblack@google.comtemplate <typename FunctionImpl> 5212983Sgabeblack@google.comclass InvokeAction { 5312983Sgabeblack@google.com public: 5412983Sgabeblack@google.com // The c'tor makes a copy of function_impl (either a function 5512983Sgabeblack@google.com // pointer or a functor). 5612983Sgabeblack@google.com explicit InvokeAction(FunctionImpl function_impl) 5712983Sgabeblack@google.com : function_impl_(function_impl) {} 5812983Sgabeblack@google.com 5912983Sgabeblack@google.com template <typename Result, typename ArgumentTuple> 6012983Sgabeblack@google.com Result Perform(const ArgumentTuple& args) { 6112983Sgabeblack@google.com return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); 6213039Sgabeblack@google.com } 6313039Sgabeblack@google.com 6413039Sgabeblack@google.com private: 6513039Sgabeblack@google.com FunctionImpl function_impl_; 6613039Sgabeblack@google.com 6713039Sgabeblack@google.com GTEST_DISALLOW_ASSIGN_(InvokeAction); 6813039Sgabeblack@google.com}; 6913039Sgabeblack@google.com 7013039Sgabeblack@google.com// Implements the Invoke(object_ptr, &Class::Method) action. 7113039Sgabeblack@google.comtemplate <class Class, typename MethodPtr> 7213039Sgabeblack@google.comclass InvokeMethodAction { 7313039Sgabeblack@google.com public: 7413039Sgabeblack@google.com InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) 7513039Sgabeblack@google.com : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {} 7613039Sgabeblack@google.com 7712986Sgabeblack@google.com template <typename Result, typename ArgumentTuple> 7813039Sgabeblack@google.com Result Perform(const ArgumentTuple& args) const { 7912986Sgabeblack@google.com return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( 8013039Sgabeblack@google.com obj_ptr_, method_ptr_, args); 8113039Sgabeblack@google.com } 8213039Sgabeblack@google.com 8313039Sgabeblack@google.com private: 8413039Sgabeblack@google.com // The order of these members matters. Reversing the order can trigger 8513039Sgabeblack@google.com // warning C4121 in MSVC (see 8613039Sgabeblack@google.com // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ). 8712986Sgabeblack@google.com const MethodPtr method_ptr_; 8813039Sgabeblack@google.com Class* const obj_ptr_; 8913039Sgabeblack@google.com 9013039Sgabeblack@google.com GTEST_DISALLOW_ASSIGN_(InvokeMethodAction); 9112986Sgabeblack@google.com}; 9212986Sgabeblack@google.com 9312986Sgabeblack@google.com// An internal replacement for std::copy which mimics its behavior. This is 9413039Sgabeblack@google.com// necessary because Visual Studio deprecates ::std::copy, issuing warning 4996. 9513039Sgabeblack@google.com// However Visual Studio 2010 and later do not honor #pragmas which disable that 9613039Sgabeblack@google.com// warning. 9713039Sgabeblack@google.comtemplate<typename InputIterator, typename OutputIterator> 9812986Sgabeblack@google.cominline OutputIterator CopyElements(InputIterator first, 9912986Sgabeblack@google.com InputIterator last, 10013039Sgabeblack@google.com OutputIterator output) { 10113039Sgabeblack@google.com for (; first != last; ++first, ++output) { 10213039Sgabeblack@google.com *output = *first; 10313039Sgabeblack@google.com } 10413039Sgabeblack@google.com return output; 10513039Sgabeblack@google.com} 10613039Sgabeblack@google.com 10713039Sgabeblack@google.com} // namespace internal 10813039Sgabeblack@google.com 10913039Sgabeblack@google.com// Various overloads for Invoke(). 11013039Sgabeblack@google.com 11113039Sgabeblack@google.com// Creates an action that invokes 'function_impl' with the mock 11213039Sgabeblack@google.com// function's arguments. 11313039Sgabeblack@google.comtemplate <typename FunctionImpl> 11413039Sgabeblack@google.comPolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( 11513039Sgabeblack@google.com FunctionImpl function_impl) { 11613039Sgabeblack@google.com return MakePolymorphicAction( 11713039Sgabeblack@google.com internal::InvokeAction<FunctionImpl>(function_impl)); 11813039Sgabeblack@google.com} 11913039Sgabeblack@google.com 12013039Sgabeblack@google.com// Creates an action that invokes the given method on the given object 12113039Sgabeblack@google.com// with the mock function's arguments. 12213039Sgabeblack@google.comtemplate <class Class, typename MethodPtr> 12313039Sgabeblack@google.comPolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( 12413039Sgabeblack@google.com Class* obj_ptr, MethodPtr method_ptr) { 12513039Sgabeblack@google.com return MakePolymorphicAction( 12613039Sgabeblack@google.com internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); 12713039Sgabeblack@google.com} 12813039Sgabeblack@google.com 12913039Sgabeblack@google.com// WithoutArgs(inner_action) can be used in a mock function with a 13013039Sgabeblack@google.com// non-empty argument list to perform inner_action, which takes no 13113039Sgabeblack@google.com// argument. In other words, it adapts an action accepting no 13213039Sgabeblack@google.com// argument to one that accepts (and ignores) arguments. 13313039Sgabeblack@google.comtemplate <typename InnerAction> 13413039Sgabeblack@google.cominline internal::WithArgsAction<InnerAction> 13513039Sgabeblack@google.comWithoutArgs(const InnerAction& action) { 13613039Sgabeblack@google.com return internal::WithArgsAction<InnerAction>(action); 13713124Sgabeblack@google.com} 13813124Sgabeblack@google.com 13912983Sgabeblack@google.com// WithArg<k>(an_action) creates an action that passes the k-th 14012983Sgabeblack@google.com// (0-based) argument of the mock function to an_action and performs 14112983Sgabeblack@google.com// it. It adapts an action accepting one argument to one that accepts 14212983Sgabeblack@google.com// multiple arguments. For convenience, we also provide 14312983Sgabeblack@google.com// WithArgs<k>(an_action) (defined below) as a synonym. 14412983Sgabeblack@google.comtemplate <int k, typename InnerAction> 14512983Sgabeblack@google.cominline internal::WithArgsAction<InnerAction, k> 14613039Sgabeblack@google.comWithArg(const InnerAction& action) { 14713039Sgabeblack@google.com return internal::WithArgsAction<InnerAction, k>(action); 14812837Sgabeblack@google.com} 14912837Sgabeblack@google.com 15012983Sgabeblack@google.com// The ACTION*() macros trigger warning C4100 (unreferenced formal 15112837Sgabeblack@google.com// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in 15212983Sgabeblack@google.com// the macro definition, as the warnings are generated when the macro 15312837Sgabeblack@google.com// is expanded and macro expansion cannot contain #pragma. Therefore 15412837Sgabeblack@google.com// we suppress them here. 15513057Sgabeblack@google.com#ifdef _MSC_VER 15612925Sgabeblack@google.com# pragma warning(push) 15713124Sgabeblack@google.com# pragma warning(disable:4100) 15813124Sgabeblack@google.com#endif 15913124Sgabeblack@google.com 16013124Sgabeblack@google.com// Action ReturnArg<k>() returns the k-th argument of the mock function. 16113124Sgabeblack@google.comACTION_TEMPLATE(ReturnArg, 16212925Sgabeblack@google.com HAS_1_TEMPLATE_PARAMS(int, k), 16312925Sgabeblack@google.com AND_0_VALUE_PARAMS()) { 16413057Sgabeblack@google.com return ::testing::get<k>(args); 16512925Sgabeblack@google.com} 16613124Sgabeblack@google.com 16713124Sgabeblack@google.com// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the 16813124Sgabeblack@google.com// mock function to *pointer. 16913124Sgabeblack@google.comACTION_TEMPLATE(SaveArg, 17013124Sgabeblack@google.com HAS_1_TEMPLATE_PARAMS(int, k), 17112925Sgabeblack@google.com AND_1_VALUE_PARAMS(pointer)) { 17212925Sgabeblack@google.com *pointer = ::testing::get<k>(args); 17312837Sgabeblack@google.com} 17412983Sgabeblack@google.com 17512837Sgabeblack@google.com// Action SaveArgPointee<k>(pointer) saves the value pointed to 17612983Sgabeblack@google.com// by the k-th (0-based) argument of the mock function to *pointer. 17712837Sgabeblack@google.comACTION_TEMPLATE(SaveArgPointee, 17812837Sgabeblack@google.com HAS_1_TEMPLATE_PARAMS(int, k), 17912837Sgabeblack@google.com AND_1_VALUE_PARAMS(pointer)) { 18012837Sgabeblack@google.com *pointer = *::testing::get<k>(args); 18112837Sgabeblack@google.com} 18212837Sgabeblack@google.com 18312983Sgabeblack@google.com// Action SetArgReferee<k>(value) assigns 'value' to the variable 18412837Sgabeblack@google.com// referenced by the k-th (0-based) argument of the mock function. 18512837Sgabeblack@google.comACTION_TEMPLATE(SetArgReferee, 18612837Sgabeblack@google.com HAS_1_TEMPLATE_PARAMS(int, k), 18712837Sgabeblack@google.com AND_1_VALUE_PARAMS(value)) { 18812837Sgabeblack@google.com typedef typename ::testing::tuple_element<k, args_type>::type argk_type; 18913040Sgabeblack@google.com // Ensures that argument #k is a reference. If you get a compiler 19012837Sgabeblack@google.com // error on the next line, you are using SetArgReferee<k>(value) in 19112837Sgabeblack@google.com // a mock function whose k-th (0-based) argument is not a reference. 19212837Sgabeblack@google.com GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, 19312837Sgabeblack@google.com SetArgReferee_must_be_used_with_a_reference_argument); 19413057Sgabeblack@google.com ::testing::get<k>(args) = value; 19513057Sgabeblack@google.com} 19613057Sgabeblack@google.com 19713057Sgabeblack@google.com// Action SetArrayArgument<k>(first, last) copies the elements in 19812837Sgabeblack@google.com// source range [first, last) to the array pointed to by the k-th 19912837Sgabeblack@google.com// (0-based) argument, which can be either a pointer or an 20012837Sgabeblack@google.com// iterator. The action does not take ownership of the elements in the 20112837Sgabeblack@google.com// source range. 20212837Sgabeblack@google.comACTION_TEMPLATE(SetArrayArgument, 20312837Sgabeblack@google.com HAS_1_TEMPLATE_PARAMS(int, k), 20412837Sgabeblack@google.com AND_2_VALUE_PARAMS(first, last)) { 20512837Sgabeblack@google.com // Visual Studio deprecates ::std::copy, so we use our own copy in that case. 20612837Sgabeblack@google.com#ifdef _MSC_VER 20712837Sgabeblack@google.com internal::CopyElements(first, last, ::testing::get<k>(args)); 20812983Sgabeblack@google.com#else 20912837Sgabeblack@google.com ::std::copy(first, last, ::testing::get<k>(args)); 21012983Sgabeblack@google.com#endif 21112837Sgabeblack@google.com} 21212837Sgabeblack@google.com 21312837Sgabeblack@google.com// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock 21412983Sgabeblack@google.com// function. 21512837Sgabeblack@google.comACTION_TEMPLATE(DeleteArg, 21612983Sgabeblack@google.com HAS_1_TEMPLATE_PARAMS(int, k), 21712837Sgabeblack@google.com AND_0_VALUE_PARAMS()) { 21812837Sgabeblack@google.com delete ::testing::get<k>(args); 21912837Sgabeblack@google.com} 22012983Sgabeblack@google.com 22112837Sgabeblack@google.com// This action returns the value pointed to by 'pointer'. 22212983Sgabeblack@google.comACTION_P(ReturnPointee, pointer) { return *pointer; } 22312837Sgabeblack@google.com 22412837Sgabeblack@google.com// Action Throw(exception) can be used in a mock function of any type 22512837Sgabeblack@google.com// to throw the given exception. Any copyable value can be thrown. 22612983Sgabeblack@google.com#if GTEST_HAS_EXCEPTIONS 22712837Sgabeblack@google.com 22812983Sgabeblack@google.com// Suppresses the 'unreachable code' warning that VC generates in opt modes. 22912837Sgabeblack@google.com# ifdef _MSC_VER 23012837Sgabeblack@google.com# pragma warning(push) // Saves the current warning state. 23112837Sgabeblack@google.com# pragma warning(disable:4702) // Temporarily disables warning 4702. 23212983Sgabeblack@google.com# endif 23312837Sgabeblack@google.comACTION_P(Throw, exception) { throw exception; } 23412983Sgabeblack@google.com# ifdef _MSC_VER 23512837Sgabeblack@google.com# pragma warning(pop) // Restores the warning state. 23612837Sgabeblack@google.com# endif 23712837Sgabeblack@google.com 23812983Sgabeblack@google.com#endif // GTEST_HAS_EXCEPTIONS 23912837Sgabeblack@google.com 24012983Sgabeblack@google.com#ifdef _MSC_VER 24112837Sgabeblack@google.com# pragma warning(pop) 24212837Sgabeblack@google.com#endif 24312837Sgabeblack@google.com 24412983Sgabeblack@google.com} // namespace testing 24512837Sgabeblack@google.com 24612983Sgabeblack@google.com#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ 24712837Sgabeblack@google.com