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