blob: d42484aef2bca540b98b72b52ef0804bb89a2bd7 [file] [log] [blame]
zhanyong.wana18423e2009-07-22 23:58:19 +00001// 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 Civila3c0dd02018-08-14 14:04:07 -040029
zhanyong.wana18423e2009-07-22 23:58:19 +000030
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 Civil984cba32018-07-27 11:15:08 -040035// GOOGLETEST_CM0002 DO NOT DELETE
36
zhanyong.wana18423e2009-07-22 23:58:19 +000037#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39
zhanyong.wan2321b2a2010-10-14 06:51:27 +000040#include <algorithm>
Abseil Team0adeadd2019-01-16 15:23:44 -050041#include <type_traits>
zhanyong.wan2321b2a2010-10-14 06:51:27 +000042
zhanyong.wan53e08c42010-09-14 05:38:21 +000043#include "gmock/gmock-generated-actions.h"
zhanyong.wana18423e2009-07-22 23:58:19 +000044
45namespace testing {
46namespace internal {
47
kosakb93d0f12014-01-13 22:28:01 +000048// 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.
52template<typename InputIterator, typename OutputIterator>
53inline 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.wana18423e2009-07-22 23:58:19 +000062} // namespace internal
63
64// Various overloads for Invoke().
65
zhanyong.wan32de5f52009-12-23 00:13:23 +000066// 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.wan658ac0b2011-02-24 07:29:13 +000072# pragma warning(push)
73# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +000074#endif
75
zhanyong.wana18423e2009-07-22 23:58:19 +000076// Action ReturnArg<k>() returns the k-th argument of the mock function.
77ACTION_TEMPLATE(ReturnArg,
78 HAS_1_TEMPLATE_PARAMS(int, k),
79 AND_0_VALUE_PARAMS()) {
Abseil Team7d3b73c2018-10-09 14:50:26 -040080 return ::std::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +000081}
82
83// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
84// mock function to *pointer.
85ACTION_TEMPLATE(SaveArg,
86 HAS_1_TEMPLATE_PARAMS(int, k),
87 AND_1_VALUE_PARAMS(pointer)) {
Abseil Team7d3b73c2018-10-09 14:50:26 -040088 *pointer = ::std::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +000089}
90
zhanyong.wan2321b2a2010-10-14 06:51:27 +000091// Action SaveArgPointee<k>(pointer) saves the value pointed to
92// by the k-th (0-based) argument of the mock function to *pointer.
93ACTION_TEMPLATE(SaveArgPointee,
94 HAS_1_TEMPLATE_PARAMS(int, k),
95 AND_1_VALUE_PARAMS(pointer)) {
Abseil Team7d3b73c2018-10-09 14:50:26 -040096 *pointer = *::std::get<k>(args);
zhanyong.wan2321b2a2010-10-14 06:51:27 +000097}
98
zhanyong.wana18423e2009-07-22 23:58:19 +000099// Action SetArgReferee<k>(value) assigns 'value' to the variable
100// referenced by the k-th (0-based) argument of the mock function.
101ACTION_TEMPLATE(SetArgReferee,
102 HAS_1_TEMPLATE_PARAMS(int, k),
103 AND_1_VALUE_PARAMS(value)) {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400104 typedef typename ::std::tuple_element<k, args_type>::type argk_type;
zhanyong.wana18423e2009-07-22 23:58:19 +0000105 // 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.
Krystian Kuzniarekda76d012019-08-14 13:33:13 +0200108 GTEST_COMPILE_ASSERT_(std::is_reference<argk_type>::value,
zhanyong.wana18423e2009-07-22 23:58:19 +0000109 SetArgReferee_must_be_used_with_a_reference_argument);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400110 ::std::get<k>(args) = value;
zhanyong.wana18423e2009-07-22 23:58:19 +0000111}
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.
118ACTION_TEMPLATE(SetArrayArgument,
119 HAS_1_TEMPLATE_PARAMS(int, k),
120 AND_2_VALUE_PARAMS(first, last)) {
kosakb93d0f12014-01-13 22:28:01 +0000121 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
zhanyong.wana18423e2009-07-22 23:58:19 +0000122#ifdef _MSC_VER
Abseil Team7d3b73c2018-10-09 14:50:26 -0400123 internal::CopyElements(first, last, ::std::get<k>(args));
kosakb93d0f12014-01-13 22:28:01 +0000124#else
Abseil Team7d3b73c2018-10-09 14:50:26 -0400125 ::std::copy(first, last, ::std::get<k>(args));
zhanyong.wana18423e2009-07-22 23:58:19 +0000126#endif
127}
128
129// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
130// function.
131ACTION_TEMPLATE(DeleteArg,
132 HAS_1_TEMPLATE_PARAMS(int, k),
133 AND_0_VALUE_PARAMS()) {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400134 delete ::std::get<k>(args);
zhanyong.wana18423e2009-07-22 23:58:19 +0000135}
136
zhanyong.wane3bd0982010-07-03 00:16:42 +0000137// This action returns the value pointed to by 'pointer'.
138ACTION_P(ReturnPointee, pointer) { return *pointer; }
139
zhanyong.wana18423e2009-07-22 23:58:19 +0000140// 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
vladloseve2e8ba42010-05-13 18:16:03 +0000143
144// Suppresses the 'unreachable code' warning that VC generates in opt modes.
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000145# ifdef _MSC_VER
146# pragma warning(push) // Saves the current warning state.
147# pragma warning(disable:4702) // Temporarily disables warning 4702.
148# endif
zhanyong.wana18423e2009-07-22 23:58:19 +0000149ACTION_P(Throw, exception) { throw exception; }
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000150# ifdef _MSC_VER
151# pragma warning(pop) // Restores the warning state.
152# endif
vladloseve2e8ba42010-05-13 18:16:03 +0000153
zhanyong.wana18423e2009-07-22 23:58:19 +0000154#endif // GTEST_HAS_EXCEPTIONS
155
zhanyong.wan32de5f52009-12-23 00:13:23 +0000156#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000157# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +0000158#endif
159
zhanyong.wana18423e2009-07-22 23:58:19 +0000160} // namespace testing
161
162#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_