| 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 |
|
| 31 |
// Google Mock - a framework for writing C++ mock classes. |
| 32 |
// |
| 33 |
// This file implements some actions that depend on gmock-generated-actions.h. |
| 34 |
|
| 35 |
// GOOGLETEST_CM0002 DO NOT DELETE |
| 36 |
|
| 37 |
// IWYU pragma: private, include "gmock/gmock.h" |
| 38 |
// IWYU pragma: friend gmock/.* |
| 39 |
|
| 40 |
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ |
| 41 |
#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ |
| 42 |
|
| 43 |
#include <algorithm> |
| 44 |
#include <type_traits> |
| 45 |
|
| 46 |
#include "gmock/gmock-generated-actions.h" |
| 47 |
|
| 48 |
namespace testing { |
| 49 |
namespace internal { |
| 50 |
|
| 51 |
// An internal replacement for std::copy which mimics its behavior. This is |
| 52 |
// necessary because Visual Studio deprecates ::std::copy, issuing warning 4996. |
| 53 |
// However Visual Studio 2010 and later do not honor #pragmas which disable that |
| 54 |
// warning. |
| 55 |
template<typename InputIterator, typename OutputIterator> |
| 56 |
inline OutputIterator CopyElements(InputIterator first, |
| 57 |
InputIterator last, |
| 58 |
OutputIterator output) { |
| 59 |
for (; first != last; ++first, ++output) { |
| 60 |
*output = *first; |
| 61 |
} |
| 62 |
return output; |
| 63 |
} |
| 64 |
|
| 65 |
} // namespace internal |
| 66 |
|
| 67 |
// Various overloads for Invoke(). |
| 68 |
|
| 69 |
// The ACTION*() macros trigger warning C4100 (unreferenced formal |
| 70 |
// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in |
| 71 |
// the macro definition, as the warnings are generated when the macro |
| 72 |
// is expanded and macro expansion cannot contain #pragma. Therefore |
| 73 |
// we suppress them here. |
| 74 |
#ifdef _MSC_VER |
| 75 |
# pragma warning(push) |
| 76 |
# pragma warning(disable:4100) |
| 77 |
#endif |
| 78 |
|
| 79 |
// Action ReturnArg<k>() returns the k-th argument of the mock function. |
| 80 |
ACTION_TEMPLATE(ReturnArg, |
| 81 |
HAS_1_TEMPLATE_PARAMS(int, k), |
| 82 |
AND_0_VALUE_PARAMS()) { |
| 83 |
return ::std::get<k>(args); |
| 84 |
} |
| 85 |
|
| 86 |
// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the |
| 87 |
// mock function to *pointer. |
| 88 |
ACTION_TEMPLATE(SaveArg, |
| 89 |
HAS_1_TEMPLATE_PARAMS(int, k), |
| 90 |
AND_1_VALUE_PARAMS(pointer)) { |
| 91 |
*pointer = ::std::get<k>(args); |
| 92 |
} |
| 93 |
|
| 94 |
// Action SaveArgPointee<k>(pointer) saves the value pointed to |
| 95 |
// by the k-th (0-based) argument of the mock function to *pointer. |
| 96 |
ACTION_TEMPLATE(SaveArgPointee, |
| 97 |
HAS_1_TEMPLATE_PARAMS(int, k), |
| 98 |
AND_1_VALUE_PARAMS(pointer)) { |
| 99 |
*pointer = *::std::get<k>(args); |
| 100 |
} |
| 101 |
|
| 102 |
// Action SetArgReferee<k>(value) assigns 'value' to the variable |
| 103 |
// referenced by the k-th (0-based) argument of the mock function. |
| 104 |
ACTION_TEMPLATE(SetArgReferee, |
| 105 |
HAS_1_TEMPLATE_PARAMS(int, k), |
| 106 |
AND_1_VALUE_PARAMS(value)) { |
| 107 |
typedef typename ::std::tuple_element<k, args_type>::type argk_type; |
| 108 |
// Ensures that argument #k is a reference. If you get a compiler |
| 109 |
// error on the next line, you are using SetArgReferee<k>(value) in |
| 110 |
// a mock function whose k-th (0-based) argument is not a reference. |
| 111 |
GTEST_COMPILE_ASSERT_(std::is_reference<argk_type>::value, |
| 112 |
SetArgReferee_must_be_used_with_a_reference_argument); |
| 113 |
::std::get<k>(args) = value; |
| 114 |
} |
| 115 |
|
| 116 |
// Action SetArrayArgument<k>(first, last) copies the elements in |
| 117 |
// source range [first, last) to the array pointed to by the k-th |
| 118 |
// (0-based) argument, which can be either a pointer or an |
| 119 |
// iterator. The action does not take ownership of the elements in the |
| 120 |
// source range. |
| 121 |
ACTION_TEMPLATE(SetArrayArgument, |
| 122 |
HAS_1_TEMPLATE_PARAMS(int, k), |
| 123 |
AND_2_VALUE_PARAMS(first, last)) { |
| 124 |
// Visual Studio deprecates ::std::copy, so we use our own copy in that case. |
| 125 |
#ifdef _MSC_VER |
| 126 |
internal::CopyElements(first, last, ::std::get<k>(args)); |
| 127 |
#else |
| 128 |
::std::copy(first, last, ::std::get<k>(args)); |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock |
| 133 |
// function. |
| 134 |
ACTION_TEMPLATE(DeleteArg, |
| 135 |
HAS_1_TEMPLATE_PARAMS(int, k), |
| 136 |
AND_0_VALUE_PARAMS()) { |
| 137 |
delete ::std::get<k>(args); |
| 138 |
} |
| 139 |
|
| 140 |
// This action returns the value pointed to by 'pointer'. |
| 141 |
ACTION_P(ReturnPointee, pointer) { return *pointer; } |
| 142 |
|
| 143 |
// Action Throw(exception) can be used in a mock function of any type |
| 144 |
// to throw the given exception. Any copyable value can be thrown. |
| 145 |
#if GTEST_HAS_EXCEPTIONS |
| 146 |
|
| 147 |
// Suppresses the 'unreachable code' warning that VC generates in opt modes. |
| 148 |
# ifdef _MSC_VER |
| 149 |
# pragma warning(push) // Saves the current warning state. |
| 150 |
# pragma warning(disable:4702) // Temporarily disables warning 4702. |
| 151 |
# endif |
| 152 |
ACTION_P(Throw, exception) { throw exception; } |
| 153 |
# ifdef _MSC_VER |
| 154 |
# pragma warning(pop) // Restores the warning state. |
| 155 |
# endif |
| 156 |
|
| 157 |
#endif // GTEST_HAS_EXCEPTIONS |
| 158 |
|
| 159 |
#ifdef _MSC_VER |
| 160 |
# pragma warning(pop) |
| 161 |
#endif |
| 162 |
|
| 163 |
} // namespace testing |
| 164 |
|
| 165 |
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ |