gmock-more-actions.h
Go to the documentation of this file.
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 // Author: wan@google.com (Zhanyong Wan)
31 
32 // Google Mock - a framework for writing C++ mock classes.
33 //
34 // This file implements some actions that depend on gmock-generated-actions.h.
35 
36 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
37 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
38 
39 #include <algorithm>
40 
42 
43 namespace testing
44 {
45 namespace internal
46 {
47 
48 // Implements the Invoke(f) action. The template argument
49 // FunctionImpl is the implementation type of f, which can be either a
50 // function pointer or a functor. Invoke(f) can be used as an
51 // Action<F> as long as f's type is compatible with F (i.e. f can be
52 // assigned to a tr1::function<F>).
53 template <typename FunctionImpl>
54 class InvokeAction
55 {
56 public:
57  // The c'tor makes a copy of function_impl (either a function
58  // pointer or a functor).
59  explicit InvokeAction(FunctionImpl function_impl)
60  : function_impl_(function_impl) {}
61 
62  template <typename Result, typename ArgumentTuple>
63  Result Perform(const ArgumentTuple & args)
64  {
66  }
67 
68 private:
69  FunctionImpl function_impl_;
70 
72 };
73 
74 // Implements the Invoke(object_ptr, &Class::Method) action.
75 template <class Class, typename MethodPtr>
77 {
78 public:
79  InvokeMethodAction(Class * obj_ptr, MethodPtr method_ptr)
80  : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {}
81 
82  template <typename Result, typename ArgumentTuple>
83  Result Perform(const ArgumentTuple & args) const
84  {
86  obj_ptr_, method_ptr_, args);
87  }
88 
89 private:
90  Class * const obj_ptr_;
91  const MethodPtr method_ptr_;
92 
94 };
95 
96 } // namespace internal
97 
98 // Various overloads for Invoke().
99 
100 // Creates an action that invokes 'function_impl' with the mock
101 // function's arguments.
102 template <typename FunctionImpl>
104  FunctionImpl function_impl)
105 {
106  return MakePolymorphicAction(
107  internal::InvokeAction<FunctionImpl>(function_impl));
108 }
109 
110 // Creates an action that invokes the given method on the given object
111 // with the mock function's arguments.
112 template <class Class, typename MethodPtr>
114  Class * obj_ptr, MethodPtr method_ptr)
115 {
116  return MakePolymorphicAction(
118 }
119 
120 // WithoutArgs(inner_action) can be used in a mock function with a
121 // non-empty argument list to perform inner_action, which takes no
122 // argument. In other words, it adapts an action accepting no
123 // argument to one that accepts (and ignores) arguments.
124 template <typename InnerAction>
126 WithoutArgs(const InnerAction & action)
127 {
129 }
130 
131 // WithArg<k>(an_action) creates an action that passes the k-th
132 // (0-based) argument of the mock function to an_action and performs
133 // it. It adapts an action accepting one argument to one that accepts
134 // multiple arguments. For convenience, we also provide
135 // WithArgs<k>(an_action) (defined below) as a synonym.
136 template <int k, typename InnerAction>
138 WithArg(const InnerAction & action)
139 {
141 }
142 
143 // The ACTION*() macros trigger warning C4100 (unreferenced formal
144 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
145 // the macro definition, as the warnings are generated when the macro
146 // is expanded and macro expansion cannot contain #pragma. Therefore
147 // we suppress them here.
148 #ifdef _MSC_VER
149 # pragma warning(push)
150 # pragma warning(disable:4100)
151 #endif
152 
153 // Action ReturnArg<k>() returns the k-th argument of the mock function.
154 ACTION_TEMPLATE(ReturnArg,
155  HAS_1_TEMPLATE_PARAMS(int, k),
156  AND_0_VALUE_PARAMS())
157 {
158  return std::tr1::get<k>(args);
159 }
160 
161 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
162 // mock function to *pointer.
163 ACTION_TEMPLATE(SaveArg,
164  HAS_1_TEMPLATE_PARAMS(int, k),
165  AND_1_VALUE_PARAMS(pointer))
166 {
167  *pointer = ::std::tr1::get<k>(args);
168 }
169 
170 // Action SaveArgPointee<k>(pointer) saves the value pointed to
171 // by the k-th (0-based) argument of the mock function to *pointer.
172 ACTION_TEMPLATE(SaveArgPointee,
173  HAS_1_TEMPLATE_PARAMS(int, k),
174  AND_1_VALUE_PARAMS(pointer))
175 {
176  *pointer = *::std::tr1::get<k>(args);
177 }
178 
179 // Action SetArgReferee<k>(value) assigns 'value' to the variable
180 // referenced by the k-th (0-based) argument of the mock function.
181 ACTION_TEMPLATE(SetArgReferee,
182  HAS_1_TEMPLATE_PARAMS(int, k),
183  AND_1_VALUE_PARAMS(value))
184 {
185  typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type;
186  // Ensures that argument #k is a reference. If you get a compiler
187  // error on the next line, you are using SetArgReferee<k>(value) in
188  // a mock function whose k-th (0-based) argument is not a reference.
190  SetArgReferee_must_be_used_with_a_reference_argument);
191  ::std::tr1::get<k>(args) = value;
192 }
193 
194 // Action SetArrayArgument<k>(first, last) copies the elements in
195 // source range [first, last) to the array pointed to by the k-th
196 // (0-based) argument, which can be either a pointer or an
197 // iterator. The action does not take ownership of the elements in the
198 // source range.
199 ACTION_TEMPLATE(SetArrayArgument,
200  HAS_1_TEMPLATE_PARAMS(int, k),
201  AND_2_VALUE_PARAMS(first, last))
202 {
203  // Microsoft compiler deprecates ::std::copy, so we want to suppress warning
204  // 4996 (Function call with parameters that may be unsafe) there.
205 #ifdef _MSC_VER
206 # pragma warning(push) // Saves the current warning state.
207 # pragma warning(disable:4996) // Temporarily disables warning 4996.
208 #endif
209  ::std::copy(first, last, ::std::tr1::get<k>(args));
210 #ifdef _MSC_VER
211 # pragma warning(pop) // Restores the warning state.
212 #endif
213 }
214 
215 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
216 // function.
217 ACTION_TEMPLATE(DeleteArg,
218  HAS_1_TEMPLATE_PARAMS(int, k),
219  AND_0_VALUE_PARAMS())
220 {
221  delete ::std::tr1::get<k>(args);
222 }
223 
224 // This action returns the value pointed to by 'pointer'.
225 ACTION_P(ReturnPointee, pointer) { return *pointer; }
226 
227 // Action Throw(exception) can be used in a mock function of any type
228 // to throw the given exception. Any copyable value can be thrown.
229 #if GTEST_HAS_EXCEPTIONS
230 
231 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
232 # ifdef _MSC_VER
233 # pragma warning(push) // Saves the current warning state.
234 # pragma warning(disable:4702) // Temporarily disables warning 4702.
235 # endif
236 ACTION_P(Throw, exception) { throw exception; }
237 # ifdef _MSC_VER
238 # pragma warning(pop) // Restores the warning state.
239 # endif
240 
241 #endif // GTEST_HAS_EXCEPTIONS
242 
243 #ifdef _MSC_VER
244 # pragma warning(pop)
245 #endif
246 
247 } // namespace testing
248 
249 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
InvokeMethodAction(Class *obj_ptr, MethodPtr method_ptr)
PolymorphicAction< Impl > MakePolymorphicAction(const Impl &impl)
internal::WithArgsAction< InnerAction > WithoutArgs(const InnerAction &action)
Result Perform(const ArgumentTuple &args) const
#define GTEST_COMPILE_ASSERT_(expr, msg)
InvokeAction(FunctionImpl function_impl)
ACTION_TEMPLATE(InvokeArgument, HAS_1_TEMPLATE_PARAMS(int, k), AND_0_VALUE_PARAMS())
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
Result Perform(const ArgumentTuple &args)
internal::WithArgsAction< InnerAction, k > WithArg(const InnerAction &action)
ACTION_P(ReturnPointee, pointer)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:07