• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
39 
40 #include <algorithm>
41 #include <type_traits>
42 
43 #include "gmock/gmock-generated-actions.h"
44 
45 namespace testing {
46 namespace internal {
47 
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>
CopyElements(InputIterator first,InputIterator last,OutputIterator output)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 
62 }  // namespace internal
63 
64 // Various overloads for Invoke().
65 
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
72 # pragma warning(push)
73 # pragma warning(disable:4100)
74 #endif
75 
76 // Action ReturnArg<k>() returns the k-th argument of the mock function.
ACTION_TEMPLATE(ReturnArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_0_VALUE_PARAMS ())77 ACTION_TEMPLATE(ReturnArg,
78                 HAS_1_TEMPLATE_PARAMS(int, k),
79                 AND_0_VALUE_PARAMS()) {
80   return ::std::get<k>(args);
81 }
82 
83 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
84 // mock function to *pointer.
ACTION_TEMPLATE(SaveArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (pointer))85 ACTION_TEMPLATE(SaveArg,
86                 HAS_1_TEMPLATE_PARAMS(int, k),
87                 AND_1_VALUE_PARAMS(pointer)) {
88   *pointer = ::std::get<k>(args);
89 }
90 
91 // Action SaveArgPointee<k>(pointer) saves the value pointed to
92 // by the k-th (0-based) argument of the mock function to *pointer.
ACTION_TEMPLATE(SaveArgPointee,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (pointer))93 ACTION_TEMPLATE(SaveArgPointee,
94                 HAS_1_TEMPLATE_PARAMS(int, k),
95                 AND_1_VALUE_PARAMS(pointer)) {
96   *pointer = *::std::get<k>(args);
97 }
98 
99 // Action SetArgReferee<k>(value) assigns 'value' to the variable
100 // referenced by the k-th (0-based) argument of the mock function.
ACTION_TEMPLATE(SetArgReferee,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (value))101 ACTION_TEMPLATE(SetArgReferee,
102                 HAS_1_TEMPLATE_PARAMS(int, k),
103                 AND_1_VALUE_PARAMS(value)) {
104   typedef typename ::std::tuple_element<k, args_type>::type argk_type;
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.
108   GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
109                         SetArgReferee_must_be_used_with_a_reference_argument);
110   ::std::get<k>(args) = value;
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.
ACTION_TEMPLATE(SetArrayArgument,HAS_1_TEMPLATE_PARAMS (int,k),AND_2_VALUE_PARAMS (first,last))118 ACTION_TEMPLATE(SetArrayArgument,
119                 HAS_1_TEMPLATE_PARAMS(int, k),
120                 AND_2_VALUE_PARAMS(first, last)) {
121   // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
122 #ifdef _MSC_VER
123   internal::CopyElements(first, last, ::std::get<k>(args));
124 #else
125   ::std::copy(first, last, ::std::get<k>(args));
126 #endif
127 }
128 
129 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
130 // function.
ACTION_TEMPLATE(DeleteArg,HAS_1_TEMPLATE_PARAMS (int,k),AND_0_VALUE_PARAMS ())131 ACTION_TEMPLATE(DeleteArg,
132                 HAS_1_TEMPLATE_PARAMS(int, k),
133                 AND_0_VALUE_PARAMS()) {
134   delete ::std::get<k>(args);
135 }
136 
137 // This action returns the value pointed to by 'pointer'.
ACTION_P(ReturnPointee,pointer)138 ACTION_P(ReturnPointee, pointer) { return *pointer; }
139 
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
143 
144 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
145 # ifdef _MSC_VER
146 #  pragma warning(push)          // Saves the current warning state.
147 #  pragma warning(disable:4702)  // Temporarily disables warning 4702.
148 # endif
ACTION_P(Throw,exception)149 ACTION_P(Throw, exception) { throw exception; }
150 # ifdef _MSC_VER
151 #  pragma warning(pop)           // Restores the warning state.
152 # endif
153 
154 #endif  // GTEST_HAS_EXCEPTIONS
155 
156 #ifdef _MSC_VER
157 # pragma warning(pop)
158 #endif
159 
160 }  // namespace testing
161 
162 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
163